当前位置:Gxlcms > 数据库问题 > MongoDB文档(一)--插入、更新、删除

MongoDB文档(一)--插入、更新、删除

时间:2021-07-01 10:21:17 帮助过:26人阅读

<collectionName>.insert(document) # 插入1个文档 db.<collectionName>.insertOne(document) # 插入多个文档 db.<collectionName>.insertMany(document)


测试1 :使用db.<collectionName>.insert向集合bolg里面插入1笔数据

  1. <span style="color: rgb(0, 0, 0);">> db.blog.insert({
  2. ... title:"Linux 物理卷(PV)、逻辑卷(LV)、卷组(VG)管理",
  3. ... Link:"https://www.cnblogs.com/lijiaman/p/12885649.html",
  4. ... summary:"(一)相关概念逻辑卷是使用逻辑卷组管理(Logic Volume Manager)创建出来的设备,如果要了解逻辑卷,那么首先...",
  5. ... tags:["Linux","study"],
  6. ... post:"2020-05-13 23:17",
  7. ... views:57,
  8. ... comments:[
  9. ... {user:"user1",
  10. ... message:"mark!",
  11. ... like:0
  12. ... }
  13. ... ],
  14. ... }
  15. ... )
  16. WriteResult({ "nInserted" : 1 })</span>


测试2 :结合数组,使用db.<collectionName>.insert向集合bolg里面插入2笔数据

  1. <span style="color: rgb(0, 0, 0);">> db.blog.insert([
  2. ... {
  3. ... title:"如何为Linux服务器添加磁盘",
  4. ... Link:"https://www.cnblogs.com/lijiaman/p/12885028.html",
  5. ... summary:"Linux服务器如果磁盘不够用了,就需要增加新的磁盘,磁盘添加到使用通常有4个步骤...",
  6. ... tags:["Linux","study"],
  7. ... post:"2020-05-13 21:31",
  8. ... views:25,
  9. ... comments:""
  10. ... } ,
  11. ... {
  12. ... title:"MySQL闪回工具--MyFlash",
  13. ... Link:"https://www.cnblogs.com/lijiaman/p/12770415.html",
  14. ... summary:"MyFlash介绍 MyFlash是美团开发的一个回滚DML操作的工具,该工具是开源的...",
  15. ... tags:["mysql","study"],
  16. ... post:"2020-04-24 21:38",
  17. ... views:23,
  18. ... comments:""
  19. ... }
  20. ... ])
  21. BulkWriteResult({
  22. "writeErrors" : [ ],
  23. "writeConcernErrors" : [ ],
  24. "nInserted" : 2,
  25. "nUpserted" : 0,
  26. "nMatched" : 0,
  27. "nModified" : 0,
  28. "nRemoved" : 0,
  29. "upserted" : [ ]
  30. })
  31. ></span>

insertone()和insertMany()方法与insert()方法使用相同,不再演示。


(二)更新文档
(2.1)使用update()方法更新文档
语法为:

  1. db.<span style="color: rgb(128, 128, 128);"><</span>collectionName<span style="color: rgb(128, 128, 128);">></span>.<span style="color: rgb(0, 0, 255);">update</span>({修改前匹配文档},$<span style="color: rgb(0, 0, 255);">set</span>{修改字段});

默认情况下,MongoDB只会更新一个文档,如果要更新多个文档,需要添加关键字{multi:ture},语法为:

  1. db.<span style="color: rgb(128, 128, 128);"><</span>collectionName<span style="color: rgb(128, 128, 128);">></span>.<span style="color: rgb(0, 0, 255);">update</span>({修改前匹配文档},$<span style="color: rgb(0, 0, 255);">set</span>{修改字段},{multi:true})


例如:更新文档“如何为Linux服务器添加磁盘”的评论信息

  1. db.blog.<span style="color: rgb(0, 0, 255);">update</span>({title:"如何为Linux服务器添加磁盘"},{$<span style="color: rgb(0, 0, 255);">set</span>:{comments:{<span style="color: rgb(255, 0, 255);">user</span>:"user2",message:"学习了",<span style="color: rgb(128, 128, 128);">like</span>:<span style="color: rgb(128, 0, 0); font-weight: bold;">5</span>}}})

确认结果:

  1. <span style="color: rgb(0, 0, 0);">> db.blog.find({title:"如何为Linux服务器添加磁盘"}).pretty()
  2. {
  3. "_id" : ObjectId("5ebd72d8c50e24a9d8fb2a7c"),
  4. "title" : "如何为Linux服务器添加磁盘",
  5. "Link" : "https://www.cnblogs.com/lijiaman/p/12885028.html",
  6. "summary" : "Linux服务器如果磁盘不够用了,就需要增加新的磁盘,磁盘添加到使用通常有4个步骤...",
  7. "tags" : [
  8. "Linux",
  9. "study"
  10. ],
  11. "post" : "2020-05-13 21:31",
  12. "views" : 25,
  13. "comments" : {
  14. "user" : "user2",
  15. "message" : "学习了",
  16. "like" : 5
  17. }
  18. }
  19. ></span>

(2.2)使用save()方法替换文档
语法为:

  1. db.<span style="color: rgb(128, 128, 128);"><</span>collectionName<span style="color: rgb(128, 128, 128);">></span>.<span style="color: rgb(0, 0, 255);">save</span>({_id:ObjectId(),NEW_DATA})

例子:将文档id为: ObjectId("5ebf7eeac48a7d8eac21ca5c")的文档替换

  1. <span style="color: rgb(0, 0, 0);">db.blog.save({"_id":ObjectId("5ebf7eeac48a7d8eac21ca5c"),
  2. "title" : "使用binlog2sql工具来恢复数据库",
  3. "Link" : "https://www.cnblogs.com/lijiaman/p/12770397.html",
  4. "summary" : "binlog2sql是国内MySQL大佬danfengcao开发,许多MySQL爱好者参与改进的一款MySQL binlog解析软件...",
  5. "tags" : [
  6. "Mysql",
  7. "study"
  8. ],
  9. "post" : "2020-04-24 21:35",
  10. "views" : 183,
  11. "comments" : [
  12. {
  13. "user" : "user1",
  14. "message" : "good!",
  15. "like" : 0
  16. }
  17. ]
  18. })</span>


(三)删除文档
语法为:

  1. db.<span style="color: rgb(0, 0, 255);"><</span><span style="color: rgb(128, 0, 0);">collectionName</span><span style="color: rgb(0, 0, 255);">></span>.remove({符合条件的文档},justOne)

remove()方法带2个可选参数:
第1个用于匹配符合条件的文档
justOne:如果为true或1,则只删除一个文档
如果2个可选参数都不填写,则删除所有数据,相当于关系型数据库的truncate命令。

例子1:删除mycol集合中id为4的文档

  1. <span style="color: rgb(0, 0, 0);">> db.mycol.find().pretty()
  2. { "_id" : ObjectId("5ebf87fcf287ff79c4d2eb61"), "id" : 1, "name" : "a" }
  3. { "_id" : ObjectId("5ebf882df287ff79c4d2eb62"), "id" : 2, "name" : "b" }
  4. { "_id" : ObjectId("5ebf882df287ff79c4d2eb63"), "id" : 3, "name" : "c" }
  5. { "_id" : ObjectId("5ebf882ef287ff79c4d2eb64"), "id" : 4, "name" : "d" }
  6. /*执行删除命令*/
  7. > db.mycol.remove({"id":4})
  8. WriteResult({ "nRemoved" : 1 })
  9. > db.mycol.find().pretty()
  10. { "_id" : ObjectId("5ebf87fcf287ff79c4d2eb61"), "id" : 1, "name" : "a" }
  11. { "_id" : ObjectId("5ebf882df287ff79c4d2eb62"), "id" : 2, "name" : "b" }
  12. { "_id" : ObjectId("5ebf882df287ff79c4d2eb63"), "id" : 3, "name" : "c" }</span>


例子2:删除mycol集合中id大于1的文档,只删除一个文档

  1. <span style="color: rgb(0, 0, 0);">> db.mycol.find().pretty()
  2. { "_id" : ObjectId("5ebf87fcf287ff79c4d2eb61"), "id" : 1, "name" : "a" }
  3. { "_id" : ObjectId("5ebf882df287ff79c4d2eb62"), "id" : 2, "name" : "b" }
  4. { "_id" : ObjectId("5ebf882df287ff79c4d2eb63"), "id" : 3, "name" : "c" }
  5. >
  6. /*执行删除命令,只删除1个文档*/
  7. > db.mycol.remove({"id":{$gt:1}},true)
  8. WriteResult({ "nRemoved" : 1 })
  9. > db.mycol.find().pretty()
  10. { "_id" : ObjectId("5ebf87fcf287ff79c4d2eb61"), "id" : 1, "name" : "a" }
  11. { "_id" : ObjectId("5ebf882df287ff79c4d2eb63"), "id" : 3, "name" : "c" }</span>


例子3:删除mycol集合中所有文档

  1. <span style="color: rgb(0, 0, 0);">> db.mycol.find({})
  2. { "_id" : ObjectId("5ebf87fcf287ff79c4d2eb61"), "id" : 1, "name" : "a" }
  3. { "_id" : ObjectId("5ebf882df287ff79c4d2eb63"), "id" : 3, "name" : "c" }
  4. <font color="#ff0000">/*备注:删除所有文档,要加上大括号{},有的文档里面不加大括号,在mongodb 3.4里面执行报错*/</font>
  5. > db.mycol.remove({})
  6. WriteResult({ "nRemoved" : 2 })
  7. > db.mycol.find({})</span>


【完】

MongoDB文档(一)--插入、更新、删除

标签:boa   cat   info   lin   man   ODB   str   inline   需要   

人气教程排行