Mathers
Mathers

Reputation: 184

MongoDB 3.2.6 failed to run updateMany()

I am new to MongoDB. I was trying to update many records in my MongoDB. But it threw me this error. Can anyone help out of here?

2017-04-13T11:23:07.572-0700 E QUERY [thread1] Error: the update operation document must contain atomic operators : DBCollection.prototype.updateMany@src/mongo/shell/crud_api.js:568:1 @(shell):1:1

Sample data:

{
    userId: "id#1",
    username: "abc",
    firstName: "ABC",
    lastName: "DEF",
    age: 19
}
{
    userId: "id#1",
    username: "abc",
    firstName: "ABC",
    lastName: "DEF",
    age: 19
}
{
    userId: "id#1",
    username: "abc",
    firstName: "ABC",
    lastName: "DEF",
    age: 19
}
{
    userId: "id#1",
    username: "abc",
    firstName: "ABC",
    lastName: "DEF",
    age: 19
}

Sample query:

db.users.updateMany(
{
    userId: {
        $in: ["id#1","id#2","id#3"]
    }
},
{
    userId: "id#12345",
    username: "abc",
    firstName: "ABC",
    lastName: "DEF",
    age: 19
})

Upvotes: 1

Views: 1115

Answers (1)

user886
user886

Reputation: 1197

updateMany requires you to specify the operation you are applying. Your sample query would then look like:

db.users.updateMany(
{
    userId: {
        $in: ["id#1","id#2","id#3"]
    }
},
{
  $set: {
    userId: "id#12345",
    username: "abc",
    firstName: "ABC",
    lastName: "DEF",
    age: 19
  }
})

Upvotes: 1

Related Questions