Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

274
Views
Can MongoDB Update Aggregation Pipeline conditionally replace the document?

Given a document like;

{
  "name": "Jason",
  "foo": "bar",
  "version": 3,
  // ...
}

Is it possible to specify an update aggregation pipeline to conditionally update the entire document based on a property of the current document? i.e. to specify something like;

const newDocument = { ...currentDocument, version: 4, foo: "baz" };

if (document.version == 3) 
  $replaceRoot(newDocument)
else
  doNothing()

Post answer edit

The marked answer below does address this question. For the sake of being thorough I would like to add some context as to why this question was asked as the answer will slightly change.

CosmosDB provides the feature of Optimistic Concurrency Control (OCC) that allows CosmosDB to reject a command on a document if the document has changed unexpectedly. Important to note that the client can supply the expected version (_etag property) of the document.

MongoDB does use this feature under the hood, but does not expose OCC to the client. This means that your entire document change needs to be described in the Mongo Update pipeline syntax. This syntax is okay for simple documents and update commands. But, it is not a replacement for testable business logic code.

To expose OCC to the client when using MongoDB use a version property or similar and ensure to target the expected version in the command filter as described here.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Yes it can, you can build the update pipeline in code like you did but you can also do it in Mongo with $cond like so:

db.collection.updateOne({},
[
  {
    $replaceRoot: {
      newRoot: {
        $cond: [
          {
            $eq: [
              "$version",
              3
            ]
          },
          {
            $mergeObjects: [
              "$$ROOT",
              {
                version: 4,
                foo: "baz"
              }
            ]
          },
          "$$ROOT"
        ]
      }
    }
  }
])

Mongo Playground

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!