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

clipboard



(一)插入文档
插入文档一共有3种方法,分别如下:

# 插入一个或多个文档,如果是多个文档,用数组存放文档
db.<collectionName>.insert(document)

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

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


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

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


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

> db.blog.insert([
...   {
...   title:"如何为Linux服务器添加磁盘",
 ...   Link:"https://www.cnblogs.com/lijiaman/p/12885028.html",
 ...   summary:"Linux服务器如果磁盘不够用了,就需要增加新的磁盘,磁盘添加到使用通常有4个步骤...",
 ...   tags:["Linux","study"],
 ...   post:"2020-05-13 21:31",
 ...   views:25,
 ...   comments:""
 ...   } ,
 ...   {
 ...   title:"MySQL闪回工具--MyFlash",
 ...   Link:"https://www.cnblogs.com/lijiaman/p/12770415.html",
 ...   summary:"MyFlash介绍 MyFlash是美团开发的一个回滚DML操作的工具,该工具是开源的...",
 ...   tags:["mysql","study"],
 ...   post:"2020-04-24 21:38",
 ...   views:23,
 ...   comments:""
 ...   }
 ... ])
 BulkWriteResult({
     "writeErrors" : [ ],
     "writeConcernErrors" : [ ],
     "nInserted" : 2,
     "nUpserted" : 0,
     "nMatched" : 0,
     "nModified" : 0,
     "nRemoved" : 0,
    "upserted" : [ ]
 })
 >

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


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

db.<collectionName>.update({修改前匹配文档},$set{修改字段});

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

db.<collectionName>.update({修改前匹配文档},$set{修改字段},{multi:true})


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

db.blog.update({title:"如何为Linux服务器添加磁盘"},{$set:{comments:{user:"user2",message:"学习了",like:5}}})

确认结果:

> db.blog.find({title:"如何为Linux服务器添加磁盘"}).pretty()
 {
     "_id" : ObjectId("5ebd72d8c50e24a9d8fb2a7c"),
     "title" : "如何为Linux服务器添加磁盘",
     "Link" : "https://www.cnblogs.com/lijiaman/p/12885028.html",
     "summary" : "Linux服务器如果磁盘不够用了,就需要增加新的磁盘,磁盘添加到使用通常有4个步骤...",
     "tags" : [
         "Linux",
         "study"
     ],
     "post" : "2020-05-13 21:31",
     "views" : 25,
     "comments" : {
         "user" : "user2",
         "message" : "学习了",
         "like" : 5
     }
 }
 >

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

db.<collectionName>.save({_id:ObjectId(),NEW_DATA})

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

db.blog.save({"_id":ObjectId("5ebf7eeac48a7d8eac21ca5c"),
    "title" : "使用binlog2sql工具来恢复数据库",
    "Link" : "https://www.cnblogs.com/lijiaman/p/12770397.html",
    "summary" : "binlog2sql是国内MySQL大佬danfengcao开发,许多MySQL爱好者参与改进的一款MySQL binlog解析软件...",
    "tags" : [
        "Mysql",
        "study"
    ],
    "post" : "2020-04-24 21:35",
    "views" : 183,
    "comments" : [
        {
            "user" : "user1",
            "message" : "good!",
            "like" : 0
        }
    ]
 })


(三)删除文档
语法为:

db.<collectionName>.remove({符合条件的文档},justOne)

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

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

> db.mycol.find().pretty()
 { "_id" : ObjectId("5ebf87fcf287ff79c4d2eb61"), "id" : 1, "name" : "a" }
 { "_id" : ObjectId("5ebf882df287ff79c4d2eb62"), "id" : 2, "name" : "b" }
 { "_id" : ObjectId("5ebf882df287ff79c4d2eb63"), "id" : 3, "name" : "c" }
 { "_id" : ObjectId("5ebf882ef287ff79c4d2eb64"), "id" : 4, "name" : "d" }

/*执行删除命令*/
 > db.mycol.remove({"id":4})
 WriteResult({ "nRemoved" : 1 })

> db.mycol.find().pretty()
{ "_id" : ObjectId("5ebf87fcf287ff79c4d2eb61"), "id" : 1, "name" : "a" }
 { "_id" : ObjectId("5ebf882df287ff79c4d2eb62"), "id" : 2, "name" : "b" }
 { "_id" : ObjectId("5ebf882df287ff79c4d2eb63"), "id" : 3, "name" : "c" }


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

> db.mycol.find().pretty()
 { "_id" : ObjectId("5ebf87fcf287ff79c4d2eb61"), "id" : 1, "name" : "a" }
 { "_id" : ObjectId("5ebf882df287ff79c4d2eb62"), "id" : 2, "name" : "b" }
 { "_id" : ObjectId("5ebf882df287ff79c4d2eb63"), "id" : 3, "name" : "c" }
 > 

/*执行删除命令,只删除1个文档*/
 > db.mycol.remove({"id":{$gt:1}},true)
 WriteResult({ "nRemoved" : 1 })


 > db.mycol.find().pretty()
 { "_id" : ObjectId("5ebf87fcf287ff79c4d2eb61"), "id" : 1, "name" : "a" }
 { "_id" : ObjectId("5ebf882df287ff79c4d2eb63"), "id" : 3, "name" : "c" }


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

> db.mycol.find({})
{ "_id" : ObjectId("5ebf87fcf287ff79c4d2eb61"), "id" : 1, "name" : "a" }
{ "_id" : ObjectId("5ebf882df287ff79c4d2eb63"), "id" : 3, "name" : "c" }

/*备注:删除所有文档,要加上大括号{},有的文档里面不加大括号,在mongodb 3.4里面执行报错*/
 > db.mycol.remove({})
WriteResult({ "nRemoved" : 2 })

> db.mycol.find({})


【完】