Added the option of running the query in upsert mode