Saurabh Ghewari
Saurabh Ghewari

Reputation: 709

Want to update the document in mongodb?

I have a document as follows in mongodb:

 {
     "_id" : ObjectId("53bba63f5ef7980c092dae11"),
      "traits" : [],
      "skills" : [],
      "name" : {
          "first" : "Abc",
          "last" : "abc"
      },
      "auth" : {
          "email" : "[email protected]",
          "password" : "12345678"
      },
  }

and I want to update the password. for updating I do this

db.talents.update({"_id": "53bba63f5ef7980c092dae11"}, 
    {$set : {"auth.password": "Saurabh123"}})

query executes but does not update the password. please tell me where i am making mistake?

and when I am using upsert: true it gives following error

Error : - insertDocument :: caused by :: 11000 E11000 duplicate key error index:
talent.talents.$auth.email_1  dup key: { : null }

Id and email have the unique index.

Upvotes: 4

Views: 87

Answers (1)

John Petrone
John Petrone

Reputation: 27487

You are specifying the "_id" used for the update incorrectly. When you are using MongoDB ObjectIds as you _id, when you query or update using _id you need to make a call to ObjectId().

In your case this code works:

db.talents.update({"_id" : ObjectId("53bba63f5ef7980c092dae11")}, {$set : {"auth.password" :"Saurabh123"}})

Note that I've added a call to ObjectId() wrapped around the string you were using.

Upvotes: 3

Related Questions