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

2016-12-15  本文已影响0人  yi_zhe

插入并保存文档

> db.foo.insert({"bar":"abc"})
WriteResult({ "nInserted" : 1 })
>
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

删除文档

> db.foo.find()
{ "_id" : ObjectId("58524f7a4698e19ca147b3ce"), "bar" : "abc" }
> db.foo.remove({})
WriteResult({ "nRemoved" : 1 })
> db.foo.find()
>
> 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
>

更新文档

> 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 }
>
> 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 }
>
> 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" } }
>
> 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" } }
>
> 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" ] }
>
> 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" ] }
>
> 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" ] }
>
> 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.

> 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:{"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" ] }
>
> 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" ] }
>
> 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" ] }
>
上一篇 下一篇

猜你喜欢

热点阅读