首页 > 代码库 > MongoDB文档的增删改操作

MongoDB文档的增删改操作

上一篇文章中介绍了MongoDB的一些基本知识,同时看到了怎么启动一个MongoDB服务,并且通过MongoDB自带的shell工具连接到了服务器。

这一次,就通过MongoDB shell介绍一下对文档的增删改操作。

增加新文档

接着上一篇文章,打开一个MongoDB shell。

通过“show dbs”可以看到所有的数据库。然后我们通过“use blog”切换到blog数据库来开始下面的演示。

使用“db”命令显示当前正在使用的数据库。

 1 C:\mongodb\bin>mongo.exe 2 MongoDB shell version: 2.4.6 3 connecting to: test 4 > show dbs 5 local   0.078125GB 6 > 7 > use blog 8 switched to db blog 9 >10 > db11 blog12 >

接下来就开始新建文档,插入文档的操作了

 1 > post1 = {"title":"learn MongoDB", "author":"Wilber", "date":new Date()} 2 { 3         "title" : "learn MongoDB", 4         "author" : "Wilber", 5         "date" : ISODate("2014-11-29T06:19:32.556Z") 6 } 7 > db.blog.posts.insert(post1) 8 > db.blog.posts.find() 9 { "_id" : ObjectId("5479657a421b7f1536cfb1f7"), "title" : "learn MongoDB", "author" : "Wilber", "date" : ISODate("2014-110 1-29T06:19:32.556Z") }11 > show collections12 blog.posts13 system.indexes14 >

通过相同的方式插入新的文档。

post2 = {"title":"learn English", "author":"Will", "date":new Date()}post3 = {"title":"learn C#", "author":"Li", "date":new Date()}post4 = {"title":"learn SQL", "author":"July", "date":new Date()}

 

删除文档

通过remove操作,可以对文档进行删除。如果没有任何参数,remove将会删除collection中所有的文档,而且删除操作是不可逆的,所以要小心操作。

1 > db.blog.posts.remove()2 > db.blog.posts.find()3 >

同时,remove操作可以支持条件删除。

 1 > db.blog.posts.find() 2 { "_id" : ObjectId("547967c1421b7f1536cfb1fe"), "title" : "learn MongoDB", "author" : "Wilber", "date" : ISODate("2014-1 3 1-29T06:29:21.318Z") } 4 { "_id" : ObjectId("547967c1421b7f1536cfb1ff"), "title" : "learn English", "author" : "Will", "date" : ISODate("2014-11- 5 29T06:29:21.349Z") } 6 { "_id" : ObjectId("547967c1421b7f1536cfb200"), "title" : "learn C#", "author" : "Li", "date" : ISODate("2014-11-29T06:2 7 9:21.365Z") } 8 { "_id" : ObjectId("547967c2421b7f1536cfb201"), "title" : "learn SQL", "author" : "July", "date" : ISODate("2014-11-29T0 9 6:29:21.380Z") }10 >11 >12 > db.blog.posts.remove({"author":"Will"})13 > db.blog.posts.find()14 { "_id" : ObjectId("547967c1421b7f1536cfb1fe"), "title" : "learn MongoDB", "author" : "Wilber", "date" : ISODate("2014-115 1-29T06:29:21.318Z") }16 { "_id" : ObjectId("547967c1421b7f1536cfb200"), "title" : "learn C#", "author" : "Li", "date" : ISODate("2014-11-29T06:217 9:21.365Z") }18 { "_id" : ObjectId("547967c2421b7f1536cfb201"), "title" : "learn SQL", "author" : "July", "date" : ISODate("2014-11-29T019 6:29:21.380Z") }20 >

 

更新文档

可以通过update方法更新数据库中的文档,update有两个参数,一个是查询文档,用来找出要更新的文档,另一个是修改器文档,描述多找到的文档进行怎样的更新。

文档的更新可以分为两种:文档替换和使用修改器更新

文档替换

当文档的模式变化很大的时候,一般都是采用文档替换的方式进行文档更新。

1 > post1.detailInfo = {"author":"Wilber","description":"this is a post about MongoDB"}2 { "author" : "Wilber", "description" : "this is a post about MongoDB" }3 > delete post1.author4 true5 > db.blog.posts.update({"author":"Wilber"}, post1)6 > db.blog.posts.find({"detailInfo.author":"Wilber"})7 { "_id" : ObjectId("547971c5421b7f1536cfb202"), "title" : "learn MongoDB", "date" : ISODate("2014-11-29T07:12:05.560Z"),8  "detailInfo" : { "author" : "Wilber", "description" : "this is a post about MongoDB" } }9 >

 

修改器更新

对于只需要部分更新的文档,通过修改器更新会很方便。直接上例子。

$set: 修改文档的一个指定键的值,如果没有则创建。

1 > db.blog.posts.update({"author":"Li"}, {$set: {"title":"how to learn C#"}})2 > db.blog.posts.find({"author":"Li"})3 { "_id" : ObjectId("547971c5421b7f1536cfb204"), "author" : "Li", "date" : ISODate("2014-11-29T07:12:05.591Z"), "title" :4  "how to learn C#" }5 > db.blog.posts.update({"author":"Li"}, {$set: {"age":27}})6 > db.blog.posts.find({"author":"Li"})7 { "_id" : ObjectId("547971c5421b7f1536cfb204"), "age" : 27, "author" : "Li", "date" : ISODate("2014-11-29T07:12:05.591Z"8 ), "title" : "how to learn C#" }9 >

$addToSet, $push, $pop:这三个都是对数组的操作,$addToSet能够避免重复添加

 1 > comments = [] 2 [ ] 3 > comment0 = {"score": 70, "content":"just so so"} 4 { "score" : 70, "content" : "just so so" } 5 > comment1 = {"score": 90, "content":"very good"} 6 { "score" : 90, "content" : "very good" } 7 > 8 > db.blog.posts.update({"author":"Li"}, {$set: {"comments": comments}}) 9 > db.blog.posts.update({"author":"Li"}, {$set: {"comments.0": comment0}})10 > db.blog.posts.update({"author":"Li"}, {$set: {"comments.1": comment1}})11 > db.blog.posts.update({"author":"Li"}, {$addToSet: {"comments": comment1}})12 > db.blog.posts.find({"author":"Li"})13 { "_id" : ObjectId("547971c5421b7f1536cfb204"), "age" : 27, "author" : "Li", "comments" : [     {   "score" : 70,14 "content" : "just so so" },     {       "score" : 90,   "content" : "very good" } ], "date" : ISODate("2014-11-29T07:12:15 05.591Z"), "title" : "how to learn C#" }16 > db.blog.posts.update({"author":"Li"}, {$push: {"comments": comment1}})17 > db.blog.posts.find({"author":"Li"})18 { "_id" : ObjectId("547971c5421b7f1536cfb204"), "age" : 27, "author" : "Li", "comments" : [     {   "score" : 70,19 "content" : "just so so" },     {       "score" : 90,   "content" : "very good" },      {       "score" : 90,   "content20 " : "very good" } ], "date" : ISODate("2014-11-29T07:12:05.591Z"), "title" : "how to learn C#" }21 > db.blog.posts.update({"author":"Li"}, {$pop: {"comments": comment1}})22 > db.blog.posts.find({"author":"Li"})23 { "_id" : ObjectId("547971c5421b7f1536cfb204"), "age" : 27, "author" : "Li", "comments" : [     {   "score" : 70,24 "content" : "just so so" },     {       "score" : 90,   "content" : "very good" } ], "date" : ISODate("2014-11-29T07:12:25 05.591Z"), "title" : "how to learn C#" }26 >

$inc:这个跟$set的用法类似,只不过$inc是专门针对数字操作的。

1 > db.blog.posts.update({"author":"Li"}, {$inc: {"comments.1.score": 5}})2 > db.blog.posts.find({"author":"Li"})3 { "_id" : ObjectId("547971c5421b7f1536cfb204"), "age" : 27, "author" : "Li", "comments" : [     {   "score" : 70,4 "content" : "just so so" },     {       "score" : 95,   "content" : "very good" } ], "date" : ISODate("2014-11-29T07:12:5 05.591Z"), "title" : "how to learn C#" }6 >

$unset:这个命令是用来删除指定键的。

1 > db.blog.posts.update({"author":"Li"}, {$unset: {"comments": 1}})2 > db.blog.posts.find({"author":"Li"})3 { "_id" : ObjectId("547971c5421b7f1536cfb204"), "age" : 27, "author" : "Li", "date" : ISODate("2014-11-29T07:12:05.591Z"4 ), "title" : "how to learn C#" }

 

update(arg1, arg2, arg3, arg4)

上面已经介绍了update的前两个参数的含义和用法,其实update命令还有两个参数。

arg3:这个参数代表upsert用法;如果这个参数设置为true,那么就会使用upsert的方式更新文档。

1 > db.blog.posts.update({"author":"June"}, {"author": "June", "title": "C++ introduction"}, true)2 > db.blog.posts.find({"author":"June"})3 { "_id" : ObjectId("5479781d217928d2be69552c"), "author" : "June", "title" : "C++ introduction" }4 >

arg4:这个参数可以设置是否批量更新

 1 > db.blog.posts.insert(post1) 2 > db.blog.posts.find({"detailInfo.author":"Wilber"}) 3 { "_id" : ObjectId("547971c5421b7f1536cfb202"), "title" : "learn MongoDB", "date" : ISODate("2014-11-29T07:12:05.560Z"), 4  "detailInfo" : { "author" : "Wilber", "description" : "this is a post about MongoDB" } } 5 { "_id" : ObjectId("54797864421b7f1536cfb206"), "title" : "learn MongoDB", "date" : ISODate("2014-11-29T07:12:05.560Z"), 6  "detailInfo" : { "author" : "Wilber", "description" : "this is a post about MongoDB" } } 7 > db.blog.posts.update({"detailInfo.author":"Wilber"}, {$set: {"title":"how to learn MongoDB"}}, false, true) 8 > 9 > db.blog.posts.find({"detailInfo.author":"Wilber"})10 { "_id" : ObjectId("54797864421b7f1536cfb206"), "date" : ISODate("2014-11-29T07:12:05.560Z"), "detailInfo" : { "author"11 : "Wilber", "description" : "this is a post about MongoDB" }, "title" : "how to learn MongoDB" }12 { "_id" : ObjectId("547971c5421b7f1536cfb202"), "date" : ISODate("2014-11-29T07:12:05.560Z"), "detailInfo" : { "author"13 : "Wilber", "description" : "this is a post about MongoDB" }, "title" : "how to learn MongoDB" }14 >

 

总结

这篇文章主要介绍了通过MongoDB shell对数据库文档进行增删改操作,接下来会介绍查询的操作。

Ps: 附件包含了本文中的所有命令作为参考。

http://files.cnblogs.com/wilber2013/test.js

 

MongoDB文档的增删改操作