[SERVER-53707] updateMany with pipeline throws an error while update with pipeline and multi flag doesn't Created: 12/Jan/21  Updated: 27/Oct/23  Resolved: 20/Jan/21

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

Type: Question Priority: Major - P3
Reporter: Dominik Voss Assignee: Dmitry Agranat
Resolution: Works as Designed Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

When I'm trying to run updateMany() in the following way, my server returns an error:

db.collection.updateMany(
  <query>,
  <pipeline>
);

Error: Error: the update operation document must contain atomic operators.

When I'm using update() instead, it works. Using it like so:

db.collection.update(
  <query>,
  <pipeline>,
  {
    multi: true
  }
);

I tried in on version 4.2 and 4.4 - no difference. The exact code looks like this:

Doesn't work:

db.getCollection("testsets").updateMany(
    { properties: { $exists: true } }, 
    [
        { $set: { "documentProperties.documentCategory": "$properties.documentCategory",  "documentProperties.pageCategories": "$properties.pageCategories"} },
        { $unset: ["properties.documentCategory", "properties.pageCategories"] }
    ]
);

Works:

db.getCollection("testsets").update(
    { properties: { $exists: true } }, 
    [
        { $set: { "documentProperties.documentCategory": "$properties.documentCategory",  "documentProperties.pageCategories": "$properties.pageCategories"} },
        { $unset: ["properties.documentCategory", "properties.pageCategories"] }
    ],
    {
        multi: true,
    }
);

Error:

2021-01-12T09:36:56.413+0100 E QUERY    [js] Error: the update operation document must contain atomic operators :
DBCollection.prototype.updateMany@src/mongo/shell/crud_api.js:625:1
@(shell):1:1

Thanks!



 Comments   
Comment by Dmitry Agranat [ 20/Jan/21 ]

Hi dominikvoss@mailbox.org,

Tried with MongoDB 4.4.3 (both mongod and mongo shell) and it works as designed:

> db.getCollection("testsets").updateMany(
...     { properties: { $exists: true } },
...     [
...         { $set: { "documentProperties.documentCategory": "$properties.documentCategory",  "documentProperties.pageCategories": "$properties.pageCategories"} },
...         { $unset: ["properties.documentCategory", "properties.pageCategories"] }
...     ]
... );
{ "acknowledged" : true, "matchedCount" : 0, "modifiedCount" : 0 }

I will go ahead and close this ticket but if you have any further questions, please post here and we'll reopen it.

Regards,
Dima

Comment by Dmitry Agranat [ 12/Jan/21 ]

Hi dominikvoss@mailbox.org, could you please clarify what mongo shell version was used when testing MongoDB 4.2 and 4.4?

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