Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB: How to update an entire document in a collection

Tags:

mongodb

In my application I need to edit a document... and while editing the document the current [unchanged] version should be still available to other users. For instance, when I start editing a document, I create a new copy of it in a separate collection, and when done I replace the current version with the new version of the temporary collection.

Let's assume the original document stored in collection users looks like this:

{
  "_id" : ObjectId("53b986e2fe000000019a5a13"),
  "name" : "Joe",
  "birthDate" : "2080-12-11",
  "publications" : [
    { "title" : "title 1", "description" : "description 1" },
    { "title" : "title 2", "description" : "description 2" }
  ]
}

Then, let's assume the document above is copied to another collection (e.g. users.wip) and modified like this:

{
  "_id" : ObjectId("53b986e2fe000000019a5a13"),
  "name" : "Joe",
  "birthDate" : "1980-12-11",
  "publications" : [
    { "title" : "bye bye", "description" : "Blah blah" },
    { "title" : "title 2", "description" : "description 2" },
    { "title" : "title 3", "description" : "description 3" }
  ]
}

How do I replace the whoe document? The problem is that when I try to update the original document with

{ "$set" : {
  "name" : "Joe",
  "birthDate" : "1980-12-11",
  "publications" : [
    { "title" : "bye bye", "description" : "Blah blah" },
    { "title" : "title 2", "description" : "description 2" },
    { "title" : "title 3", "description" : "description 3" }
  ]
}}

I always get the following error message:

10150  Field name duplication not allowed with modifiers

That said, what's the correct way to update an entire document?

like image 950
j3d Avatar asked Jul 12 '14 14:07

j3d


1 Answers

To replace the whole document, you don't use $set, but just provide the new doc to the update call:

db.test.update({"_id": ObjectId("53b986e2fe000000019a5a13")}, {
  "_id" : ObjectId("53b986e2fe000000019a5a13"),
  "name" : "Joe",
  "birthDate" : "1980-12-11",
  "publications" : [
    { "title" : "bye bye", "description" : "Blah blah" },
    { "title" : "title 2", "description" : "description 2" },
    { "title" : "title 3", "description" : "description 3" }
  ]
})

However, with the current 3.0 driver, it would be best to use replaceOne instead:

db.test.replaceOne({"_id": ObjectId("53b986e2fe000000019a5a13")}, {
  "name" : "Joe",
  "birthDate" : "1980-12-11",
  "publications" : [
    { "title" : "bye bye", "description" : "Blah blah" },
    { "title" : "title 2", "description" : "description 2" },
    { "title" : "title 3", "description" : "description 3" }
  ]
})
like image 62
JohnnyHK Avatar answered Oct 24 '22 11:10

JohnnyHK