Use query as basis for upsertion