MongoDB: How to update an entire document in a collection

mongodb

Solution

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" }
  ]
})

Problem

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?

Original source