美文网首页
3. 创建/更新和删除文档

3. 创建/更新和删除文档

作者: yi_zhe | 来源:发表于2016-12-15 11:47 被阅读0次

插入并保存文档

  • 使用insert向目标集合插入文档
    下面代码向db所指向的数据库的foo集合中插入一个文档
> db.foo.insert({"bar":"abc"})
WriteResult({ "nInserted" : 1 })
>
  • 使用batchInsert批量插入文档(验证后没有此方法)
db.foo1.batchInsert([{"_id":0},{"_id":1},{"_id":2}])
2016-12-15T16:11:21.173+0800 TypeError: Property 'batchInsert' of object test.foo1 is not a function

删除文档

  • 使用remove删除一个集合中的所有文档
> db.foo.find()
{ "_id" : ObjectId("58524f7a4698e19ca147b3ce"), "bar" : "abc" }
> db.foo.remove({})
WriteResult({ "nRemoved" : 1 })
> db.foo.find()
>
  • 使用drop直接删除集合
> db.foo.insert({"a":123})
WriteResult({ "nInserted" : 1 })
> db.foo.find()
{ "_id" : ObjectId("5852640f4698e19ca147b3d0"), "a" : 123 }
> db.foo.drop()
true
> db.foo.find()
> show collections
system.indexes
>

更新文档

  • 使用update方法更新文档
    update接收两个参数, 第一个参数是查询条件, 第二个参数是修改器.
> db.persons.insert({"name":"joe"})
WriteResult({ "nInserted" : 1 })
> newjoe={"name":"joe","age":25}
{ "name" : "joe", "age" : 25 }
> db.persons.update({"name":"joe"}, newjoe)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 25 }
>
  • 使用修改器$inc(只能用于整形/长整型/双进度浮点型)
    将集合中name为joe的文档中age的值增加1
> db.persons.find()
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 25 }
> db.persons.update({"name":"joe"}, {$inc:{"age":1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26 }
>
  • 使用修改器$set
    $set 修改器用来指定一个字段的值, 如果字段不存在, 则创建这个字段
    比如, 向文档中增加性别信息, 增加后发现写错了, 再更新性别信息
> db.persons.find()
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26 }
> db.persons.update({"name":"joe"}, {$set:{"sex":"male"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "male" }
> db.persons.update({"name":"joe"}, {$set:{"sex":"female"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female" }
>

$set 不仅可以更新键的值, 甚至可以更新键的类型.
下面的例子将joe喜欢的歌从字符串改为了数组

> db.persons.find()
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female" }
> db.persons.update({"name":"joe"}, {$set:{"songs":"Take me to your heart"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "songs" : "Take me to your heart" }
> db.persons.update({"name":"joe"}, {$set:{"songs":["Take me to your heart", "Pretty girl"]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "songs" : [ "Take me to your heart", "Pretty girl" ] }
>

使用$set修改器还可以修改内嵌文档, 比如joe喜欢的书的作者写错了,需要更新

> db.persons.update({"name":"joe"}, {$set:{"books":{"author":"LiYang","name":"A book"}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "books" : { "author" : "LiYang", "name" : "A book" } }
> db.persons.update({"name":"joe"}, {$set:{"books.author":"LeYang"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "books" : { "author" : "LeYang", "name" : "A book" } }
>
  • 使用修改器$unset删除键
    假如joe不喜欢任何歌了, 那么删掉songs键
> db.persons.update({"name":"joe"}, {$set:{"books":{"author":"LiYang","name":"A book"}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "books" : { "author" : "LiYang", "name" : "A book" } }
> db.persons.update({"name":"joe"}, {$set:{"books.author":"LeYang"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "books" : { "author" : "LeYang", "name" : "A book" } }
>
  • 使用数组修改器$push
    如果数组已经存在, $push会向已有数组末加入一个元素, 否则创建数组并加入元素
> db.persons.find()
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female" }
> db.persons.update({"name":"joe"}, {$push:{"comments":"comment1"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "comments" : [ "comment1" ] }
>
  • 使用$each子操作符与$push联用添加多个值
> db.persons.find()
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "comments" : [ "comment1" ] }
> db.persons.update({"name":"joe"}, {$push:{"comments":{$each:["comment2","comment3"]}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "comments" : [ "comment1", "comment2", "comment3" ] }
>
  • 使用$slice操作符限定数组长度
    下面的例子, 最多保留4个评论
> db.persons.find()
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "comments" : [ "comment1", "comment2", "comment3" ] }
> db.persons.update({"name":"joe"}, {$push:{"comments":{$each:["comment4","comment5"], $slice:-4}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "comments" : [ "comment2", "comment3", "comment4", "comment5" ] }
>
  • 使用$sort将数组中的元素排序
> db.persons.update({"name":"joe"}, {$push:{"comments":{$each:["comment9","comment6"], $slice:-4, $sort:1}}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "comments" : [ "comment4", "comment5", "comment6", "comment9" ] }
>

**注意: **不能只将$slice或$sort与$push配合使用, 且必须使用$each.

  • 使用$addToSet实现无重复元素的数组
> db.persons.find()
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "comments" : [ "comment4", "comment5", "comment6", "comment9" ] }
> db.persons.update({"name":"joe"}, {$addToSet:{"comments":"comment4"}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })
> db.persons.update({"name":"joe"}, {$addToSet:{"comments":"comment7"}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "comments" : [ "comment4", "comment5", "comment6", "comment9", "comment7" ] }
> db.persons.update({"name":"joe"}, {$addToSet:{"comments":{$each:["comment7","comment8"]}}});
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "comments" : [ "comment4", "comment5", "comment6", "comment9", "comment7", "comment8" ] }
>
  • 使用$pop从数组中删除元素
{$pop:{"key":1}} 从数组末尾删除
{$pop:{"key":-1}} 从数组头部删除
> db.persons.find()
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "comments" : [ "comment4", "comment5", "comment6", "comment9", "comment7", "comment8" ] }
> db.persons.update({"name":"joe"}, {$pop:{"comments":1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "comments" : [ "comment4", "comment5", "comment6", "comment9", "comment7" ] }
> db.persons.update({"name":"joe"}, {$pop:{"comments":-1}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "comments" : [ "comment5", "comment6", "comment9", "comment7" ] }
>
  • 使用$pull根据条件删除数组元素
> db.persons.find()
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "comments" : [ "comment5", "comment6", "comment9", "comment7" ] }
> db.persons.update({"name":"joe"}, {$pull:{"comments":"comment9"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "comments" : [ "comment5", "comment6", "comment7" ] }
>
  • 使用数组下标更新数组(从0开始)
> db.persons.find()
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "comments" : [ "comment5", "comment6", "comment7" ] }
> db.persons.update({"name":"joe"}, {$set:{"comments.0":"commentA"}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.persons.find()
{ "_id" : ObjectId("58527a364698e19ca147b3d1"), "name" : "joe", "age" : 26, "sex" : "female", "comments" : [ "commentA", "comment6", "comment7" ] }
>

相关文章

  • 3. 创建/更新和删除文档

    插入并保存文档 使用insert向目标集合插入文档下面代码向db所指向的数据库的foo集合中插入一个文档 使用ba...

  • 4、创建、更新和删除文档(MongoDB笔记)

    一、插入并保存文档 可以使用insert方法向目标集合插入一个文档: 这个操作会给文档自动增加一个"_id"键(要...

  • nodejs--day5笔记

    数据库 1. 创建集合 2. 创建文档 create方法 3.导入数据 4.查询数据 5.删除文档 6. 更新文档

  • mongodb常用命令

    一、数据库 创建数据库 删除数据库 二、集合 创建集合 删除集合 三、文档 插入文档 删除文档 更新文档 四、查询...

  • Making queries(执行查询)

    一旦你创建了你的数据模型,Django会自动为你提供一个数据库抽取API,让你创建、检索、更新和删除对象。 本文档...

  • SQL中视图

    内容 1. 视图概念. 视图的创建,更新和删除. 2. 如何使用视图来简化SQL操作. 3. 视图和临时表的区别,...

  • Elasticsearch 7.x 入门之restful请求(2

    使用restful操作 创建文档新建文档.png 查看文档文档信息.png 删除文档文档删除.png 新建文档自动...

  • MongoDB基本操作

    创建数据库 删除数据库 插入文档 删除文档 参数说明: query:可选,删除的文档的条件 justOne:可选,...

  • mongodb数据库操作

    1. 创建数据库 2. 查看所有的数据库 3. 删除数据库 4.创建集合 5.文档插入

  • MongoDB-第三章-创建、更新和删除文档

    创建、更新和删除文档 使用一款数据库,最主要的就是对其(关系或集合)进行增删改查,接下来就介绍如何在MongoDB...

网友评论

      本文标题:3. 创建/更新和删除文档

      本文链接:https://www.haomeiwen.com/subject/heaimttx.html