29

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": ""
}

4 Answers 4

33

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.

Sign up to request clarification or add additional context in comments.

Comments

16

Changed in version 3.6. Following is the syntax for update :

db.collection.update(
   <query>,
   <update>,
   {
     upsert: <boolean>,
     multi: <boolean>,
     writeConcern: <document>,
     collation: <document>,
     arrayFilters: [ <filterdocument1>, ... ]
   }
)

Example :

db.getCollection('products').update({},{$unset: {translate:1, qordoba_translation_version:1}}, {multi: true})

In your example :

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

By default, the update() method updates a single document. Set the Multi Parameter to update all documents that match the query criteria.

Example 2 :

   db.checkin_4e95ae0926abe9ad28000001.update(
       {location_city:"New York"}, //query
       // $update query
       { $set : { location_country: "FUDGE!"}}, {multi: true});

Comments

9
db.m_country.update(
    {"countryId": "962a0935-bf3d-4f63-a53c-254760273ede"}, 
    {$set: {'countryPopulation': '12540000'}})

Comments

0

Before Update

> db.student.find({name:"Venky"}).pretty();

{
    "_id" : ObjectId("6012e64dc2979ddffe1e5df9"),
    "name" : "Venky",
    "dept" : "MCA",
    "age" : "26",
    "phone" : "89786465"

}

Update Command

> db.student.update({name:"Venky"},{$set: {name:"DODDANNA CHAWAN",dept:"MCA(CS)", age:"25", phone:"1234567890"}});

Find Command See Result

> db.student.find({name:"DODDANNA CHAWAN"}).pretty();

After Updated Result

{
    "_id" : ObjectId("6012e64dc2979ddffe1e5df9"),
    "name" : "DODDANNA CHAWAN",
    "dept" : "MCA(CS)",
    "age" : "25",
    "phone" : "1234567890"

}

in real life use unique "_id" to match the document becuase names will found as duplicates

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.