Reputation: 32331
I am trying to update a row in Mongo DB . I have a collection named users
db.users.find()
{ "_id" : ObjectId("50e2efe968caee13be412413"), "username" : "sss", "age" : 32 }
I am trying to update the row with the username as Erinamodobo and modify the age to 55
I have tried the below way , but its not working .
db.users.update({ "_id": "50e2efe968caee13be412413" }, { $set: { "username": "Erinamodobo" } });
Please let me know where i am making the mistake ??
Upvotes: 2
Views: 333
Reputation: 43884
As an extension to @WiredPrairie, even though he states the right answer he doesn't really explain.
The OjbectId
is not a string, it is actually a Object
so to search by that Object
you must supply an Ojbect
of the same type, i.e. here an ObjectId
:
db.users.update({ "_id": ObjectId("50e2efe968caee13be412413") }, { $set: { "username": "Erinamodobo" } });
The same goes for any specific BSON type you use from Date
to NumberLong
you will need to wrap the parameters in Object
s of the type.
Upvotes: 0
Reputation: 59763
Pass in the _id as an ObjectId if you're using the mongo shell, otherwise, it won't find the existing user.
db.users.update({"_id": ObjectId("50e2efe968caee13be412413")},
{ "$set" :
{ "username": "Erinamodobo", "age" : "55" }})
Upvotes: 2
Reputation: 1499
With this query you are updating the name itself.
Notice that the syntax of an update is the following:
db.COLLECTION.update( {query}, {update}, {options} )
where query
selects the record to update and update
specify the value of the field to update.
So, in your case the correct command is:
db.users.update({ "name": "Erinamodobo" }, { $set: { "age": 55 } });
However, I suggets you to read the mongodb documentation, is very well written (http://docs.mongodb.org/manual/applications/update/)
Upvotes: 1