本文旨在梳理mongodb的crud操作,达到快速掌握的目的。

一、C(Create) - insert
插入数据比较简单

先创建一个局部变量article,然后插入数据

1
2
3
4
mongoshell> article = {title:"t1",content:"c1"}
{ "title" : "t1", "content" : "c1" }
mongoshell> db.article.insert(article)
WriteResult({ "nInserted" : 1 })

或者直接插入BSON对象

1
2
mongoshell> db.article.insert({title:"t1",content:"c1"})
WriteResult({ "nInserted" : 1 })

用for循环创建多条数据

1
2
mongoshell> for(i=1;i<100;i++)db.article.insert({article_id:i,title:"tttt"})
WriteResult({ "nInserted" : 1 })

二、R(Retrieve) - find / findOne

查询所有数据

1
2
3
4
5
6
mongoshell> db.article.find()
{ "_id" : ObjectId("54c8c3c9a7a818872690ef04"), "article_id" : 1, "title" : "tttt" }
{ "_id" : ObjectId("54c8c3c9a7a818872690ef05"), "article_id" : 2, "title" : "tttt" }
{ "_id" : ObjectId("54c8c3c9a7a818872690ef06"), "article_id" : 3, "title" : "tttt" }
...
Type "it" for more

键入it继续显示

1
2
3
4
5
6
mongoshell> it
{ "_id" : ObjectId("54c8c3c9a7a818872690ef18"), "article_id" : 21, "title" : "tttt" }
{ "_id" : ObjectId("54c8c3c9a7a818872690ef19"), "article_id" : 22, "title" : "tttt" }
{ "_id" : ObjectId("54c8c3c9a7a818872690ef1a"), "article_id" : 23, "title" : "tttt" }
...
Type "it" for more

显示一条数据

1
2
mongoshell> db.article.findOne()
{ "_id" : ObjectId("54c8c259b14b5ac2d180425d"), "title" : "tttt" }

按照条件查询多条

1
2
mongoshell> db.article.find({article_id:5})
{ "_id" : ObjectId("54c8c3c9a7a818872690ef08"), "article_id" : 5, "title" : "tttt" }

按照条件查询一条

1
2
3
4
5
6
mongoshell> db.article.findOne({article_id:5})
{
"_id" : ObjectId("54c8c3c9a7a818872690ef08"),
"article_id" : 5,
"title" : "tttt"
}

过滤条件 skip limit sort等

1
2
3
4
mongoshell> db.article.find().skip(3).limit(4).sort({article_id:1})
{ "_id" : ObjectId("54c8c3c9a7a818872690ef06"), "article_id" : 3, "title" : "tttt" }
{ "_id" : ObjectId("54c8c3c9a7a818872690ef07"), "article_id" : 4, "title" : "tttt" }
{ "_id" : ObjectId("54c8c3c9a7a818872690ef08"), "article_id" : 5, "title" : "tttt" }

三、U(Update) - update

3.1 update的四个参数:
格式:

1
db.collection.update(查询条件,更新数据,记录不存在是否添加,是否多文档操作)

更新一条记录

1
mongoshell> db.article.update({title:'t'},{title:'tt'})

3.2 指定第三个参数为true,在没有符合条件的结果时,会自动增加一条记录

1
2
3
4
5
6
7
8
9
mongodbshell> db.article.update({title:"aaa"},{title:"ccc"},true)
WriteResult({
"nMatched" : 0,
"nUpserted" : 1,
"nModified" : 0,
"_id" : ObjectId("54c8c259b14b5ac2d180425d")
})
mongoshell> db.article.find({title:"ccc"})
{ "_id" : ObjectId("54c8c259b14b5ac2d180425d"), "title" : "ccc" }

3.3 更新指定字段数据需要使用$set操作符,否则将会替换整条记录

例如:

1
2
3
4
5
6
mongoshell> db.article.update({title:'tttt'},{title:'mmmmmm'})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
mongoshell> db.article.find().limit(5)
{ "_id" : ObjectId("54c8c3c9a7a818872690ef04"), "title" : "mmmmmm" }
{ "_id" : ObjectId("54c8c3c9a7a818872690ef05"), "article_id" : 2, "title" : "tttt" }
{ "_id" : ObjectId("54c8c3c9a7a818872690ef06"), "article_id" : 3, "title" : "tttt" }

应该添加$set操作符

1
2
3
4
5
6
mongoshell> db.article.update({title:'tttt'},{$set:{title:'mmmmmm'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
mongoshell> db.article.find().limit(5)
{ "_id" : ObjectId("54c8c3c9a7a818872690ef04"), "article_id" : 1, "title" : "mmmmmm" }
{ "_id" : ObjectId("54c8c3c9a7a818872690ef05"), "article_id" : 2, "title" : "tttt" }
{ "_id" : ObjectId("54c8c3c9a7a818872690ef06"), "article_id" : 3, "title" : "tttt" }

3.4 update默认只更新符合条件的第一条数据,除非指定第四个参数为true

例如:更新title为’tttt’的记录,默认只更新符合条件的第一条

1
2
3
4
5
6
mongoshell> db.article.update({title:'tttt'},{$set:{title:'oooo'}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
mongoshell> db.article.find().limit(5)
{ "_id" : ObjectId("54c8c3c9a7a818872690ef04"), "article_id" : 1, "title" : "oooo" }
{ "_id" : ObjectId("54c8c3c9a7a818872690ef05"), "article_id" : 2, "title" : "tttt" }
{ "_id" : ObjectId("54c8c3c9a7a818872690ef06"), "article_id" : 3, "title" : "tttt" }

开启多文档更新,更新所有符合条件的记录

1
2
3
4
5
6
mongoshell> db.article.update({title:'tttt'},{$set:{title:'oooo'}},false,true)
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
mongoshell> db.article.find().limit(5)
{ "_id" : ObjectId("54c8c3c9a7a818872690ef04"), "article_id" : 1, "title" : "oooo" }
{ "_id" : ObjectId("54c8c3c9a7a818872690ef05"), "article_id" : 2, "title" : "oooo" }
{ "_id" : ObjectId("54c8c3c9a7a818872690ef06"), "article_id" : 3, "title" : "oooo" }

3.4 多文档操作需要使用\$set操作符 (例子同上)

四、D(Delete) - remove

4.1 必须传递参数,指定删除条件,否则报错(设计目的是为了防止误操作删除数据)

1
2
mongoshell> db.article.remove()
2015-01-28T11:54:42.189+0000 remove needs a query at src/mongo/shell/collection.js:299

4.2 跟update不同,默认会删除所有符合条件的记录

1
2
mongoshell> db.article.remove({title:'tttt'})
WriteResult({ "nRemoved" : 98 })

五、其他常用命令

5.1 显示数据库 show dbs

1
2
3
4
5
mongoshell> show dbs
admin (empty)
blog 0.078GB
local 0.078GB
test (empty)

5.2 显示数据集 show collections / show tables

1
2
3
4
5
6
7
8
mongoshell> show collections
article
user
system.indexes
mongoshell> show tables
article
user
system.indexes

5.4 删除数据集

1
2
3
4
5
mongoshell> db.article.drop()
true
mongoshell> show collections
user
system.indexes

5.3 删除数据库

1
2
3
4
5
6
7
8
9
10
11
12
13
mongoshell> show dbs
admin (empty)
blog 0.078GB
local 0.078GB
test (empty)
mongoshell> use blog
switched to db blog
mongoshell> db.dropDatabase()
{ "dropped" : "blog", "ok" : 1 }
mongoshell> show dbs
admin (empty)
local 0.078GB
test (empty)