[DOCS-6515] document update sample need acorrection Created: 03/Nov/15  Updated: 11/Jan/17  Resolved: 03/Nov/15

Status: Closed
Project: Documentation
Component/s: manual
Affects Version/s: mongodb-3.0
Fix Version/s: 01112017-cleanup

Type: Improvement Priority: Minor - P4
Reporter: John Du Assignee: Kay Kim (Inactive)
Resolution: Cannot Reproduce Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

CentOS 7.1, MongoDB shell version: 3.0.6, AWS EC2


Participants:
Days since reply: 8 years, 15 weeks, 1 day ago

 Description   

The issue was found in this document url: https://docs.mongodb.org/getting-started/shell/update/#update-data-with-the-mongo-shell

Where "Replace a Document" section the sample code not working:
in the document"
"After the following update, the modified document will only contain the _id field, name field, the address field. i.e. the document will not contain the restaurant_id, cuisine, grades, and the borough fields.

db.restaurants.update(

{ "restaurant_id" : "41704620" }

,
{
"name" : "Vella 2",
"address" :

{ "coord" : [ -73.9557413, 40.7720266 ], "building" : "1480", "street" : "2 Avenue", "zipcode" : "10075" }

}
)
"
the document did not get replaced but only the fields updated. Adding "restaurant_id" : "41704620" in the new document section, the document got replaced. The sample code should be as following:

db.restaurants.update(

{ "restaurant_id" : "41704620" }

,
{
"name" : "Vella 2",
"address" :

{ "restaurant_id" : "41704620", "coord" : [ -73.9557413, 40.7720266 ], "building" : "1480", "street" : "2 Avenue", "zipcode" : "10075" }

}
)



 Comments   
Comment by John Du [ 03/Nov/15 ]

I Kay,

That was my fault: there were two document had the same restaurant id
number and same information. Only difference was their document "_id".

Sorry for the confuse.

Thanks for your time.

Regards,

John Du

630-788-6708

Comment by Kay Kim (Inactive) [ 03/Nov/15 ]

Hi –
cannot reproduce the issue:
So, if we

> db.restaurants.update( 
     { "restaurant_id" : "41704620" },   
     {  "name" : "Vella 2", "address" : {   "coord" : [ -73.9557413, 40.7720266 ], "building" : "1480",  "street" : "2 Avenue",               "zipcode" : "10075"      }    }
 )
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })

Then, because the document has been replaced and the replacement document did not contain a "restaurant_id" field, the following operation does not match any document:

> db.restaurants.find( { "restaurant_id":  "41704620" } )

However, doing a find by the new name "Vella 2" does return the document

> db.restaurants.find( { name: "Vella 2" } )
{ "_id" : ObjectId("56382bf1562f5efdbd3d3788"), "name" : "Vella 2", "address" : { "coord" : [ -73.9557413, 40.7720266 ], "building" : "1480", "street" : "2 Avenue", "zipcode" : "10075" } }

Comment by John Du [ 03/Nov/15 ]

Not sure how could "restaurant_id" : "41704620" be shown as an element of the "address" in the description - jira bug?

Comment by John Du [ 03/Nov/15 ]

the sample code should be:

db.restaurants.update(

{ "restaurant_id" : "41704620" }

,
{
"restaurant_id" : "41704620",
"name" : "Vella 2",
"address" :

{ "coord" : [ -73.9557413, 40.7720266 ], "building" : "1480", "street" : "2 Avenue", "zipcode" : "10075" }

}
)

Generated at Thu Feb 08 07:52:27 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.