MongoDB update document

an introduction
MongoDB uses the update() and save() methods to update documents in the collection.
 
Two update() method
1. The update() method is used to update an existing document. The syntax format is as follows:
db.collection.update(
   <query>,
   <update>,
   {
     upsert: <boolean>,
     multi: <boolean>,
     writeConcern: <document>
   }
)
Parameter Description:
query : The query condition for update, similar to the where in the sql update query.
update : The object of update and some update operators (such as $, $inc...), etc., can also be understood as the part after the set in the sql update query
upsert : optional, this parameter means, if there is no updated record, whether to insert objNew, true is the insertion, the default is false, not inserted.
multi : optional, mongodb defaults to false, only the first record found is updated, if this parameter is true, all the records found by the condition are updated.
writeConcern : optional, the level of exception thrown.
2. Examples
We insert the following data into the collection col:
  1. > db.col.insert(document)
  2. WriteResult({ "nInserted" : 1 })
  3. > db.col.insert({
  4. ... title: 'MongoDB 教程',
  5. ... description: 'MongoDB 是一个 Nosql 数据库',
  6. ... by: '菜鸟教程',
  7. ... url: 'http://www.runoob.com',
  8. ... tags: ['mongodb', 'database', 'NoSQL'],
  9. ... likes: 100
  10. ... })
  11. WriteResult({ "nInserted" : 1 })
Then use the update() method to update the title:
  1. > db.col.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB'}})
  2. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
  3. > db.col.find().pretty()
  4. {
  5. "_id" : ObjectId("593b6160bcd6757fd2d302fd"),
  6. "title" : "MongoDB",
  7. "description" : "MongoDB 是一个 Nosql 数据库",
  8. "by" : "菜鸟教程",
  9. "url" : "http://www.runoob.com",
  10. "tags" : [
  11. "mongodb",
  12. "database",
  13. "NoSQL"
  14. ],
  15. "likes" : 100
  16. }
  17. {
  18. "_id" : ObjectId("593b61fdbcd6757fd2d302fe"),
  19. "title" : "MongoDB 教程",
  20. "description" : "MongoDB 是一个 Nosql 数据库",
  21. "by" : "菜鸟教程",
  22. "url" : "http://www.runoob.com",
  23. "tags" : [
  24. "mongodb",
  25. "database",
  26. "NoSQL"
  27. ],
  28. "likes" : 100
  29. }
  30. {
  31. "_id" : ObjectId("593b6555bcd6757fd2d302ff"),
  32. "title" : "MongoDB 教程",
  33. "description" : "MongoDB 是一个 Nosql 数据库",
  34. "by" : "菜鸟教程",
  35. "url" : "http://www.runoob.com",
  36. "tags" : [
  37. "mongodb",
  38. "database",
  39. "NoSQL"
  40. ],
  41. "likes" : 100
  42. }
You can see that the title has been updated from the original "MongoDB Tutorial" to "MongoDB".
The above statement will only modify the first found document. If you want to modify multiple identical documents, you need to set the multi parameter to true.
  1. > db.col.update({'title':'MongoDB 教程'},{$set:{'title':'MongoDB'}},{multi:true})
  2. WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
  3. > db.col.find().pretty() })
  4. {
  5. "_id" : ObjectId("593b6160bcd6757fd2d302fd"),
  6. "title" : "MongoDB",
  7. "description" : "MongoDB 是一个 Nosql 数据库",
  8. "by" : "菜鸟教程",
  9. "url" : "http://www.runoob.com",
  10. "tags" : [
  11. "mongodb",
  12. "database",
  13. "NoSQL"
  14. ],
  15. "likes" : 100
  16. }
  17. {
  18. "_id" : ObjectId("593b61fdbcd6757fd2d302fe"),
  19. "title" : "MongoDB",
  20. "description" : "MongoDB 是一个 Nosql 数据库",
  21. "by" : "菜鸟教程",
  22. "url" : "http://www.runoob.com",
  23. "tags" : [
  24. "mongodb",
  25. "database",
  26. "NoSQL"
  27. ],
  28. "likes" : 100
  29. }
  30. {
  31. "_id" : ObjectId("593b6555bcd6757fd2d302ff"),
  32. "title" : "MongoDB",
  33. "description" : "MongoDB 是一个 Nosql 数据库",
  34. "by" : "菜鸟教程",
  35. "url" : "http://www.runoob.com",
  36. "tags" : [
  37. "mongodb",
  38. "database",
  39. "NoSQL"
  40. ],
  41. "likes" : 100
  42. }
Three save() methods
1. Grammar
The save() method replaces the existing document with the passed in document. The syntax format is as follows:
db.collection.save(
   <document>,
   {
     writeConcern: <document>
   }
)
参数说明:
document : 文档数据。
writeConcern :可选,抛出异常的级别。
2、实例
以下实例中我们替换了 _id 为 593b6555bcd6757fd2d302ff 的文档数据:
  1. > db.col.save({
  2. ... "_id" : ObjectId("593b6555bcd6757fd2d302ff"),
  3. ... "title" : "MongoDB是一个很好的课程",
  4. ... "description" : "MongoDB 是一个 Nosql 数据库",
  5. ... "by" : "Runoob",
  6. ... "url" : "http://www.runoob.com",
  7. ... "tags" : [
  8. ... "mongodb",
  9. ... "NoSQL"
  10. ... ],
  11. ... "likes" : 130
  12. ... })
  13. WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
替换成功后,我们可以通过 find() 命令来查看替换后的数据
  1. > db.col.find().pretty()
  2. {
  3. "_id" : ObjectId("593b6160bcd6757fd2d302fd"),
  4. "title" : "MongoDB",
  5. "description" : "MongoDB 是一个 Nosql 数据库",
  6. "by" : "菜鸟教程",
  7. "url" : "http://www.runoob.com",
  8. "tags" : [
  9. "mongodb",
  10. "database",
  11. "NoSQL"
  12. ],
  13. "likes" : 100
  14. }
  15. {
  16. "_id" : ObjectId("593b61fdbcd6757fd2d302fe"),
  17. "title" : "MongoDB",
  18. "description" : "MongoDB 是一个 Nosql 数据库",
  19. "by" : "菜鸟教程",
  20. "url" : "http://www.runoob.com",
  21. "tags" : [
  22. "mongodb",
  23. "database",
  24. "NoSQL"
  25. ],
  26. "likes" : 100
  27. }
  28. {
  29. "_id" : ObjectId("593b6555bcd6757fd2d302ff"),
  30. "title" : "MongoDB是一个很好的课程",
  31. "description" : "MongoDB 是一个 Nosql 数据库",
  32. "by" : "Runoob",
  33. "url" : "http://www.runoob.com",
  34. "tags" : [
  35. "mongodb",
  36. "NoSQL"
  37. ],
  38. "likes" : 130
  39. }

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327073888&siteId=291194637