Replacing the content of a document does not (tutorial question)

27 views
Skip to first unread message

Terry Brugger

unread,
Feb 13, 2018, 12:48:51 AM2/13/18
to mongodb-user
Good day,

New user here, going through the tutorial, and I'm at https://docs.mongodb.com/getting-started/shell/update/ where we replace a document. I run:
> 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 })
Okay, great, looks like it worked. Only when I go to look at the new document, I get:
> db.restaurants.find({"restaurant_id":"41704620"})
{ "_id" : ObjectId("5a6570f50eb5b797c8e1c565"), "address" : { "street" : "2 Avenue", "zipcode" : "10075", "building" : "1480", "coord" : [ -73.9557413, 40.7720266 ] }, "borough" : "Manhattan", "cuisine" : "Italian", "grades" : [ { "date" : ISODate("2014-10-01T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2014-01-16T00:00:00Z"), "grade" : "B", "score" : 17 } ], "name" : "Vella", "restaurant_id" : "41704620" }
Now, I did not check what the document value was before, but it looks like it updated the address, but not the name, and it left the rest of the document even though the tutorial has the important notice, "After the update, the document only contains the field or fields in the replacement document."

So maybe I missed something... in some traditional dbs I would need to commit or such before I could query the change, so I log out, log back it, and when I try it again, it gets stranger:
> 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 })
> db.restaurants.find({"restaurant_id":"41704620"})
> db.restaurants.update(    { "restaurant_id" : "41704620" },    {      "name" : "Vella 2",      "address" : {               "coord" : [ -73.9557413, 40.7720266 ],               "building" : "1480",               "street" : "2 Avenue",               "zipcode" : "10075"      }    } )
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })

So now it looks like it removed the document without replacing it. This presents two questions:
  1. Any ideas why my update was leaving original data?
  2. Any ideas why my document disappeared?
I am running:
zow@V:/mnt/c/Windows/System32$ mongo --version
MongoDB shell version v3.6.2
git version: 489d177dbd0f0420a8ca04d39fd78d0a2c539420
OpenSSL version: OpenSSL 1.0.2g  1 Mar 2016
allocator: tcmalloc
modules: none
build environment:
    distmod: ubuntu1604
    distarch: x86_64
    target_arch: x86_64
zow@V:/mnt/c/Windows/System32$ uname -a
Linux V 4.4.0-43-Microsoft #1-Microsoft Wed Dec 31 14:42:53 PST 2014 x86_64 x86_64 x86_64 GNU/Linux

Thanks in advance!
Terry

Kevin Adistambha

unread,
Feb 20, 2018, 11:56:27 PM2/20/18
to mongodb-user

Hi Terry

Any ideas why my update was leaving original data?

Without the $set operator, the update() statement will replace the whole document. Your query should not result in a partial replacement of a document. Are you certain that the series of command you posted is correct?

Any ideas why my document disappeared?

The document didn’t disappear. It was replaced. This is because in the update() statement, you specified a replacement document, but you searched for the same document using a field that was overwritten by the update. If you search for the document using its _id field instead of restaurant_id, you would see the updated document. For example:

> db.test.find()
{
  "_id": 0,
  "a": 1
}

My test collection contains one document.

> db.test.update({a:1}, {b:1})
Updated 1 existing record(s) in 6ms
WriteResult({
  "nMatched": 1,
  "nUpserted": 0,
  "nModified": 1
})

This will update a document containing the field a:1, and replace the whole document with b:1

> db.test.find()
{
  "_id": 0,
  "b": 1
}

Note that this is the same document that used to contain a:1. Now it contains b:1, but with the same _id:0 field (which shows that this is the one and the same document, since the _id field is immutable).

On another note, the command mongo --version will print the version of the mongo shell instead of the version of the server. To print the server’s version, you can use the command db.version() from inside the mongo shell.

Best regards
Kevin

Terry Brugger

unread,
Feb 27, 2018, 1:43:46 PM2/27/18
to mongodb-user
Kevin,

All great points -- makes perfect sense. Thank you for the explanation!

Terry
Reply all
Reply to author
Forward
0 new messages