Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update query in MongoDB shell

In the shell, my query is:

db.checkin_4e95ae0926abe9ad28000001.update({location_city:"New York"}, {location_country: "FUDGE!"}); 

However, it doesn't actually update my records. It doesn't error either. When I do a db.checkin_4e95ae0926abe9ad28000001.find({location_city:"New York"}); after running this, I get all my results but the location_country has not changed:

{     "_id": ObjectId("4e970209a0290b70660009e9"),     "addedOn": ISODate("2011-10-13T15:21:45.772Z"),     "location_address1": "",     "location_city": "New York",     "location_country": "United States",     "location_latLong": {         "xLon": -74.007124,         "yLat": 40.71455     },     "location_source": "socialprofile",     "location_state": "New York",     "location_zip": "" } 
like image 445
Shamoon Avatar asked Oct 13 '11 15:10

Shamoon


People also ask

How do I update a single field in MongoDB?

To update a single field or specific fields just use the $set operator. This will update a specific field of "citiName" by value "Jakarta Pusat" that defined by $set operator.

Can we update MongoDB?

MongoDB's update() and save() methods are used to update document into a collection. The update() method updates the values in the existing document while the save() method replaces the existing document with the document passed in save() method.

What does update keyword do in MongoDB?

The update() method updates the values in the existing document in the collections of MongoDB. When you update your document the value of the _id field remains unchanged. By default, the db.

How do I update node in MongoDB?

You can update a single document using the collection. updateOne() method. The updateOne() method accepts a filter document and an update document. If the query matches documents in the collection, the method applies the updates from the update document to fields and values of them.


1 Answers

This is because in second parameter of update function you need to use $set operator to update location_country as in example below:

db.checkin_4e95ae0926abe9ad28000001.update(    {location_city:"New York"}, //find criteria    // this row contains fix with $set oper    { $set : { location_country: "FUDGE!"}});  

Here you can find a list of available update operators.

like image 64
Andrew Orsich Avatar answered Sep 25 '22 07:09

Andrew Orsich