[SERVER-59704] Support for $expr clauses in the query predicate of an upsert Created: 01/Sep/21  Updated: 06/Dec/22

Status: Backlog
Project: Core Server
Component/s: None
Affects Version/s: 5.0.2
Fix Version/s: None

Type: Improvement Priority: Major - P3
Reporter: Sylvain Chambon Assignee: Backlog - Query Optimization
Resolution: Unresolved Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
is duplicated by SERVER-65883 allow $expr in update query predicate... Closed
Related
Assigned Teams:
Query Optimization
Participants:

 Description   

We have the following use case:

  • document contains a "version" field that, for legacy reasons, is a string containing an int
  • we want to ensure that we are not overriding a newer version of the document
  • if no document is present, create it

if "version" was an int, it would be simple: updateOne({ id: XXXX, version: { $lt: newVersion }}, {$set: {...}}, { upsert: true })

as "version" is a string, we have to use: updateOne({ id: XXXX, $where: "parseInt(this.version) < newVersion", {$set: {...}}, { upsert: true })

This has pretty dramatic performance implications, though.

In this use case it would be useful to have support for $expr in the query predicate of an upsert as we could use aggregation expressions such as $toInt instead of $where. The semantics would ideally be the same as with $where - ie the $expr would be discarded when upserting rather than evaluated to figure out if there are fields to be set.

 

 



 Comments   
Comment by Asya Kamsky [ 22/Apr/22 ]

Asked about here:  https://www.mongodb.com/community/forums/t/how-to-use-mongodb-date-time-in-filter/159191/2

 

Generated at Thu Feb 08 05:47:55 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.