Shamoon
Shamoon

Reputation: 43491

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

Upvotes: 29

Views: 71770

Answers (4)

Doddanna D
Doddanna D

Reputation: 41

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

Upvotes: 0

Sagar Jadhav
Sagar Jadhav

Reputation: 1119

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

Upvotes: 9

Amitesh Bharti
Amitesh Bharti

Reputation: 15703

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});

Upvotes: 16

Andrew Orsich
Andrew Orsich

Reputation: 53685

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.

Upvotes: 33

Related Questions