插入并保存文档
- 使用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" ] }
>
网友评论