MongoDB-M101-CRUD

10gen中参与了M101课程,下面的内容是根据M101课程做的笔记。

在Mongo shell中是可以使用javascript语法的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
MongoDB shell version: 2.2.2
connecting to: test
> for (i = 0; i < 3; i++)
Sun Dec 30 16:45:58 SyntaxError: syntax error (shell):1
> for (i = 0; i < 3; i++) print("hello ! "+i)
hello ! 0
hello ! 1
hello ! 2
> x = 1
1
> z = {x:2}
{ "x" : 2 }
> z.x
2
> z["x"]
2
> y = "x"
x
> z[y]
2
> NumberInt(1)
NumberInt(1)
> NumberLong(1)+NumberInt(2)
3
> NumberInt(1)+NumberLong(2)
3
> new Date()
ISODate("2012-12-30T08:44:04.636Z")

新增

MongoDB的新增方法有两个,分别是insert()和save(),insert()和save()的区别是当插入的文档存在”_id”的时候,如果”_id”重复了insert会报错,而save不会,save会更新”_id”对应的文档的内容为新内容。不论是insert或者save,当文档中”_id”不存在的时候,系统会自动生成一个唯一的ObjectId。MongoDB存储的单个BSON文档最大是16M,超过了16M可以使用GridFS存储。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
> db.people.find()
> doc = {name:"smit"}
{ "name" : "smit" }
> db.people.insert(doc)
> db.people.insert(doc)
> db.people.find()
{ "_id" : ObjectId("50e975348c1ceeb499c26b50"), "name" : "smit" }
{ "_id" : ObjectId("50e975378c1ceeb499c26b51"), "name" : "smit" }
> doc = {_id : "1",name:"smit"}
{ "_id" : "1", "name" : "smit" }
> db.people.insert(doc)
> db.people.insert(doc)
E11000 duplicate key error index: test.people.$_id_ dup key: { : "1" }
> doc = {_id : "1",name:"smitt"}
{ "_id" : "1", "name" : "smitt" }
> db.people.insert(doc)
E11000 duplicate key error index: test.people.$_id_ dup key: { : "1" }
> db.people.save(doc)
> db.people.insert(doc)
E11000 duplicate key error index: test.people.$_id_ dup key: { : "1" }
> db.people.save(doc)
> db.people.find()
{ "_id" : ObjectId("50e975348c1ceeb499c26b50"), "name" : "smit" }
{ "_id" : ObjectId("50e975378c1ceeb499c26b51"), "name" : "smit" }
{ "_id" : "1", "name" : "smitt" }

查找

只查找一个文档用的是findOne,普通的查找用的是find。
先使用for循环在scores中插入300条记录,然后分别用findOne和find把他们查出来。使用db.scores.find().pretty()打印出来的数据是比较美观的,pretty起到的是美化的作用。

1
2
3
4
5
6
7
8
9
10
11
12
13
> db.scores.find().count()
0
> for(i=0; i<100;i++){names=["exam", "essay","quiz"]; for(j=0;j<3;j++){db.scores.insert({"student":i, "type":names[j], score:Math.round(Math.random()*100) });}}

> db.scores.find().count()
300
> db.scores.findOne()
{
"_id" : ObjectId("50e979508c1ceeb499c26c23"),
"student" : 69,
"type" : "quiz",
"score" : 98
}

查出score大于95小于等于98的所有人:db.scores.find({score:{$gt:95, $lte : 98}})

查出name中第一个字母大于B、小于F的文档:db.people.find({name : {$gt : "B", $lt :"F"} })

查出表people中存在profession字段的文档:db.people.find({ profession : {$exists : true} })

查出表people的name中包含n的文档:db.people.find({name:{$regex : "n"}})

查出表people的name中n结尾的文档:db.people.find({name:{$regex : "n$"}})

查出表people的name中n开头的文档:db.people.find({name:{$regex : "^n"}})

查出表people的name是q结尾或者age字段存在的文档:db.people.find( { $or : [ { name : { $regex : "q$"} }, { age : { $exists : true } } ] } )

查出scores中score小于50或者大于90的文档:db.scores.find({ $or : [{"score" : {$lt: 50 }}, {"score" : {$gt : 90}}] }),如果使用db.scores.find( { score : { $lt : 50 }, score : { $gt : 90 } } );只会查出score大于90的文档。

查找文档中的数组
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
> db.accounts.insert( { name : "George", favorites : ["ice cream", "pretzels"] } );
> db.accounts.insert( { name : "Howard", favorites : ["pretzels", "beer"] } );
> db.accounts.find().pretty()
{
"_id" : ObjectId("50e7a66b4ce557aaed3099a8"),
"name" : "George",
"favorites" : [
"ice cream",
"pretzels"
]
}
{
"_id" : ObjectId("50e7a68e4ce557aaed3099a9"),
"name" : "Howard",
"favorites" : [
"pretzels",
"beer"
]
}
> db.accounts.find({favorites: "beer"})
{ "_id" : ObjectId("50e7a68e4ce557aaed3099a9"), "name" : "Howard", "favorites" : [ "pretzels", "beer" ] }
> db.accounts.find({favorites: "pretzels"})
{ "_id" : ObjectId("50e7a66b4ce557aaed3099a8"), "name" : "George", "favorites" : [ "ice cream", "pretzels" ] }
{ "_id" : ObjectId("50e7a68e4ce557aaed3099a9"), "name" : "Howard", "favorites" : [ "pretzels", "beer" ] }
$in $all
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
	db.accounts.insert( { name : "Irving", favorites : ["beer", "pretzels", "cheese"] } );
db.accounts.insert( { name : "John", favorites : ["beer", "cheese"] } );
> db.accounts.find()
{ "_id" : ObjectId("50e7a66b4ce557aaed3099a8"), "name" : "George", "favorites" : [ "ice cream", "pretzels" ] }
{ "_id" : ObjectId("50e7a68e4ce557aaed3099a9"), "name" : "Howard", "favorites" : [ "pretzels", "beer" ] }
{ "_id" : ObjectId("50e7ce6c355fd8e2e6e82f80"), "name" : "Irving", "favorites" : [ "beer", "pretzels", "cheese" ] }
{ "_id" : ObjectId("50e7ce76355fd8e2e6e82f81"), "name" : "John", "favorites" : [ "beer", "cheese" ] }
> db.accounts.find({favorites : {$all : ["pretzels", "beer"]} });
{ "_id" : ObjectId("50e7a68e4ce557aaed3099a9"), "name" : "Howard", "favorites" : [ "pretzels", "beer" ] }
{ "_id" : ObjectId("50e7ce6c355fd8e2e6e82f80"), "name" : "Irving", "favorites" : [ "beer", "pretzels", "cheese" ] }
> db.accounts.find({ name : {$in : ["Howard", "John"]}} });
Sat Jan 5 14:59:03 SyntaxError: missing ) after argument list (shell):1
> db.accounts.find({ name : {$in : ["Howard", "John"]} });
{ "_id" : ObjectId("50e7a68e4ce557aaed3099a9"), "name" : "Howard", "favorites" : [ "pretzels", "beer" ] }
{ "_id" : ObjectId("50e7ce76355fd8e2e6e82f81"), "name" : "John", "favorites" : [ "beer", "cheese" ] }
> db.accounts.find({ favorites : {$in : ["ice cream", "beer"]} });
{ "_id" : ObjectId("50e7a66b4ce557aaed3099a8"), "name" : "George", "favorites" : [ "ice cream", "pretzels" ] }
{ "_id" : ObjectId("50e7a68e4ce557aaed3099a9"), "name" : "Howard", "favorites" : [ "pretzels", "beer" ] }
{ "_id" : ObjectId("50e7ce6c355fd8e2e6e82f80"), "name" : "Irving", "favorites" : [ "beer", "pretzels", "cheese" ] }
{ "_id" : ObjectId("50e7ce76355fd8e2e6e82f81"), "name" : "John", "favorites" : [ "beer", "cheese" ] }
dot notation
1
2
3
4
5
6
7
8
9
10
> db.users.insert({name : "richard", email : {work : "richard@10gen.com", personal : "kreuter@example.com"} })
> db.users.find()
{ "_id" : ObjectId("50e7e24e355fd8e2e6e82f82"), "name" : "richard", "email" : { "work" : "richard@10gen.com", "personal" : "kreuter@example.com" } }
> db.users.find({email : {work : "richard@10gen.com", personal : "kreuter@example.com"}})
{ "_id" : ObjectId("50e7e24e355fd8e2e6e82f82"), "name" : "richard", "email" : { "work" : "richard@10gen.com", "personal" : "kreuter@example.com" } }
> db.users.find({email : {personal : "kreuter@example.com", work : "richard@10gen.com"}})
>
> db.users.find({email : {personal : "kreuter@example.com"}})
> db.users.find({"email.personal" : "kreuter@example.com"})
{ "_id" : ObjectId("50e7e24e355fd8e2e6e82f82"), "name" : "richard", "email" : { "work" : "richard@10gen.com", "personal" : "kreuter@example.com" } }
Cursors

可以使用var cur = db.people.find();cursor,cur.hasNext()检查是否还有文档,cur.next()获取文档然后cur指向下一个文档。cur.sort({ name : -1}).limit(3).skip(2);根据name到排序,限制只取3条记录,跳过前面2条记录。无论sort、limit、skip的先后顺序是怎样如cur.skip(2).limit(3).sort({ name : -1});,mongodb执行的时候都是先sort,然后skip,最后才是limit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
> cur = db.people.find(); null;
null
> cur.hasNext()
true
> cur.next()
{
"_id" : ObjectId("50e00064405fe32e05c6e6c0"),
"name" : "Wendyeq",
"age" : 24,
"profession" : "hacker"
}
> while (cur.hasNext()) printjson(cur.next());
{
"_id" : ObjectId("50e001de405fe32e05c6e6c1"),
"name" : "wen",
"age" : 24,
"profession" : "hacker"
}
{ "_id" : ObjectId("50e10b8ff5448477652e2365"), "name" : 42 }
{
"_id" : ObjectId("50e82ff4803d1a0d947eb66e"),
"name" : "Fred",
"age" : 30
}
> cur = db.people.find(); null;
null
> cur.limit(2); null;
null
> while (cur.hasNext()) printjson(cur.next());
{
"_id" : ObjectId("50e00064405fe32e05c6e6c0"),
"name" : "Wendyeq",
"age" : 24,
"profession" : "hacker"
}
{
"_id" : ObjectId("50e001de405fe32e05c6e6c1"),
"name" : "wen",
"age" : 24,
"profession" : "hacker"
}
> cur = db.people.find(); null;
null
> cur.sort({ name : -1}); null;
null
> while (cur.hasNext()) printjson(cur.next());
{
"_id" : ObjectId("50e001de405fe32e05c6e6c1"),
"name" : "wen",
"age" : 24,
"profession" : "hacker"
}
{
"_id" : ObjectId("50e00064405fe32e05c6e6c0"),
"name" : "Wendyeq",
"age" : 24,
"profession" : "hacker"
}
{
"_id" : ObjectId("50e82ff4803d1a0d947eb66e"),
"name" : "Fred",
"age" : 30
}
{ "_id" : ObjectId("50e10b8ff5448477652e2365"), "name" : 42 }
> cur = db.people.find(); null;
null
> cur.sort({ name : -1}).limit(3); null;
null
> while (cur.hasNext()) printjson(cur.next());
{
"_id" : ObjectId("50e001de405fe32e05c6e6c1"),
"name" : "wen",
"age" : 24,
"profession" : "hacker"
}
{
"_id" : ObjectId("50e00064405fe32e05c6e6c0"),
"name" : "Wendyeq",
"age" : 24,
"profession" : "hacker"
}
{
"_id" : ObjectId("50e82ff4803d1a0d947eb66e"),
"name" : "Fred",
"age" : 30
}
> cur = db.people.find(); null;
null
> cur.sort({ name : -1}).limit(3).skip(2); null;
null
> while (cur.hasNext()) printjson(cur.next());
{
"_id" : ObjectId("50e82ff4803d1a0d947eb66e"),
"name" : "Fred",
"age" : 30
}
{ "_id" : ObjectId("50e10b8ff5448477652e2365"), "name" : 42 }
>
> db.people.count()
4

更新

普通的更新操作一般只会保留”_id”不变,其他的字段替换为新值或者删除也就是完全替换成新的文档。

1
2
3
4
5
6
7
8
9
10
11
> db.people.find()
{ "_id" : ObjectId("50e00064405fe32e05c6e6c0"), "name" : "Wendyeq", "age" : 24, "profession" : "hacker" }
{ "_id" : ObjectId("50e001de405fe32e05c6e6c1"), "name" : "wen", "age" : 24, "profession" : "hacker" }
{ "_id" : ObjectId("50e10b8ff5448477652e2365"), "name" : 42 }
{ "_id" : ObjectId("50e82ff4803d1a0d947eb66e"), "name" : "Fred", "age" : 30 }
> db.people.update({name : "wen"},{ name : "Thompson", salary : 50000});
> db.people.find()
{ "_id" : ObjectId("50e00064405fe32e05c6e6c0"), "name" : "Wendyeq", "age" : 24, "profession" : "hacker" }
{ "_id" : ObjectId("50e001de405fe32e05c6e6c1"), "name" : "Thompson", "salary" : 50000 }
{ "_id" : ObjectId("50e10b8ff5448477652e2365"), "name" : 42 }
{ "_id" : ObjectId("50e82ff4803d1a0d947eb66e"), "name" : "Fred", "age" : 30 }

可以使用$set来替换指定的字段的值,使用$inc对数字自增。db.people.update({name:"Fred"}, {$inc : {age : 2}})表示age加2。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
> db.people.find()
{ "_id" : ObjectId("50e00064405fe32e05c6e6c0"), "name" : "Wendyeq", "age" : 24, "profession" : "hacker" }
{ "_id" : ObjectId("50e001de405fe32e05c6e6c1"), "name" : "Thompson", "salary" : 50000 }
{ "_id" : ObjectId("50e10b8ff5448477652e2365"), "name" : 42 }
{ "_id" : ObjectId("50e82ff4803d1a0d947eb66e"), "name" : "Fred", "age" : 30 }
> db.people.update({name:"Fred"}, {$set : {age : 35}})
> db.people.find({name:"Fred"})
{ "_id" : ObjectId("50e82ff4803d1a0d947eb66e"), "name" : "Fred", "age" : 35 }
> db.people.update({name:"Fred"}, {$inc : {age : 1}})
> db.people.find({name:"Fred"})
{ "_id" : ObjectId("50e82ff4803d1a0d947eb66e"), "name" : "Fred", "age" : 36 }
> db.people.update({name:42}, {$set : {age : 35}})
> db.people.find({name:42})
{ "_id" : ObjectId("50e10b8ff5448477652e2365"), "age" : 35, "name" : 42 }

在更新中使用$unset删除不需要的字段,$unset后面的1或者-1结果都一样,不知道有什么影响。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
> db.people.find()
{ "_id" : ObjectId("50e00064405fe32e05c6e6c0"), "name" : "Wendyeq", "age" : 24, "profession" : "hacker" }
{ "_id" : ObjectId("50e001de405fe32e05c6e6c1"), "name" : "Thompson", "salary" : 50000 }
{ "_id" : ObjectId("50e82ff4803d1a0d947eb66e"), "name" : "Fred", "age" : 36 }
{ "_id" : ObjectId("50e10b8ff5448477652e2365"), "age" : 35, "name" : 42 }
> db.people.update({name : "Wendyeq"}, { $unset : { profession : 1}});
> db.people.find({name:"Wendyeq"});
{ "_id" : ObjectId("50e00064405fe32e05c6e6c0"), "age" : 24, "name" : "Wendyeq" }
> db.people.update({salary : 50000}, { $unset : { name : -1}});
> db.people.find()
{ "_id" : ObjectId("50e00064405fe32e05c6e6c0"), "age" : 24, "name" : "Wendyeq" }
{ "_id" : ObjectId("50e001de405fe32e05c6e6c1"), "salary" : 50000 }
{ "_id" : ObjectId("50e82ff4803d1a0d947eb66e"), "name" : "Fred", "age" : 36 }
{ "_id" : ObjectId("50e10b8ff5448477652e2365"), "age" : 35, "name" : 42 }

对文档中的数组属性可以使用$push,在数组最后添加一个元素。使用$pop删除一个元素,db.arrays.update( {_id : 0}, { $pop : { a : 1 } } );表示删除最后一个元素,db.arrays.update( {_id : 0}, { $pop : { a : -1 } } );表示删除第一个元素。$pushAll批量添加数组元素。$pull根据值删除数组元素。$pullAllg根据值批量删除数组元素。$addToSet添加数组中不存在的值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
> db.arrays.insert({ _id : 0, a : [1, 2, 3, 4] });
> db.arrays.find()
{ "_id" : 0, "a" : [ 1, 2, 3, 4 ] }
> db.arrays.update( {_id : 0}, { $set : { "a.2" : 5 } } );
> db.arrays.find()
{ "_id" : 0, "a" : [ 1, 2, 5, 4 ] }
> db.arrays.update( {_id : 0}, { $push : { a : 6 } } );
> db.arrays.find()
{ "_id" : 0, "a" : [ 1, 2, 5, 4, 6 ] }
> db.arrays.update( {_id : 0}, { $pop : { a : 1 } } );
> db.arrays.find()
{ "_id" : 0, "a" : [ 1, 2, 5, 4 ] }
> db.arrays.update( {_id : 0}, { $pop : { a : -1 } } );
> db.arrays.find()
{ "_id" : 0, "a" : [ 2, 5, 4 ] }
> db.arrays.update( {_id : 0}, { $pushAll : { a : [ 7, 8, 9] } } );
> db.arrays.find()
{ "_id" : 0, "a" : [ 2, 5, 4, 7, 8, 9 ] }
> db.arrays.update( {_id : 0}, { $pull : { a : 5 } } );
> db.arrays.find()
{ "_id" : 0, "a" : [ 2, 4, 7, 8, 9 ] }
> db.arrays.update( {_id : 0}, { $pullAll : { a : [2, 4, 8] } } );
> db.arrays.find()
{ "_id" : 0, "a" : [ 7, 9 ] }
> db.arrays.update( {_id : 0}, { $addToSet : { a : 5 } } );
> db.arrays.find()
{ "_id" : 0, "a" : [ 7, 9, 5 ] }
> db.arrays.update( {_id : 0}, { $addToSet : { a : 5 } } );
> db.arrays.update( {_id : 0}, { $addToSet : { a : 5 } } );
> db.arrays.find()
{ "_id" : 0, "a" : [ 7, 9, 5 ] }
> db.arrays.update( {_id : 0}, { $pushAll : { a : [ 7, 8, 9] } } );
> db.arrays.find()
{ "_id" : 0, "a" : [ 7, 9, 5, 7, 8, 9 ] }
> db.arrays.update( {_id : 0}, { $pop : { a : -2 } } );
> db.arrays.find()
{ "_id" : 0, "a" : [ 9, 5, 7, 8, 9 ] }
> db.arrays.update( {_id : 0}, { $pull : { a : 9 } } );
> db.arrays.find()
{ "_id" : 0, "a" : [ 5, 7, 8 ] }

upsert means update or insert if not present.
upsert为true表示如果没有符合条件的文档就新建一个文档,如果有符合条件的稳定就更新。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
> db.people.find()
{ "_id" : ObjectId("50e00064405fe32e05c6e6c0"), "age" : 24, "name" : "Wendyeq" }
{ "_id" : ObjectId("50e001de405fe32e05c6e6c1"), "salary" : 50000 }
{ "_id" : ObjectId("50e82ff4803d1a0d947eb66e"), "name" : "Fred", "age" : 36 }
{ "_id" : ObjectId("50e10b8ff5448477652e2365"), "age" : 35, "name" : 42 }
> db.people.update( { name : "George"}, {$set : {age : 40}} )
> db.people.find()
{ "_id" : ObjectId("50e00064405fe32e05c6e6c0"), "age" : 24, "name" : "Wendyeq" }
{ "_id" : ObjectId("50e001de405fe32e05c6e6c1"), "salary" : 50000 }
{ "_id" : ObjectId("50e82ff4803d1a0d947eb66e"), "name" : "Fred", "age" : 36 }
{ "_id" : ObjectId("50e10b8ff5448477652e2365"), "age" : 35, "name" : 42 }
> db.people.update( { name : "George"}, {$set : {age : 40}}, {upsert : true} )
> db.people.find()
{ "_id" : ObjectId("50e00064405fe32e05c6e6c0"), "age" : 24, "name" : "Wendyeq" }
{ "_id" : ObjectId("50e001de405fe32e05c6e6c1"), "salary" : 50000 }
{ "_id" : ObjectId("50e82ff4803d1a0d947eb66e"), "name" : "Fred", "age" : 36 }
{ "_id" : ObjectId("50e10b8ff5448477652e2365"), "age" : 35, "name" : 42 }
{ "_id" : ObjectId("50e8fb790b336d073077842b"), "age" : 40, "name" : "George" }
> db.people.update( { age : {$gt : 35} }, {$set : {name : "William"}}, {upsert : true} )
> db.people.find()
{ "_id" : ObjectId("50e00064405fe32e05c6e6c0"), "age" : 24, "name" : "Wendyeq" }
{ "_id" : ObjectId("50e001de405fe32e05c6e6c1"), "salary" : 50000 }
{ "_id" : ObjectId("50e10b8ff5448477652e2365"), "age" : 35, "name" : 42 }
{ "_id" : ObjectId("50e8fb790b336d073077842b"), "age" : 40, "name" : "George" }
{ "_id" : ObjectId("50e82ff4803d1a0d947eb66e"), "age" : 36, "name" : "William" }

multi为true的话可以批量添加字段。

1
2
3
4
5
6
7
8
9
10
11
12
13
> db.people.find()
{ "_id" : ObjectId("50e00064405fe32e05c6e6c0"), "age" : 24, "name" : "Wendyeq" }
{ "_id" : ObjectId("50e001de405fe32e05c6e6c1"), "salary" : 50000 }
{ "_id" : ObjectId("50e10b8ff5448477652e2365"), "age" : 35, "name" : 42 }
{ "_id" : ObjectId("50e8fb790b336d073077842b"), "age" : 40, "name" : "George" }
{ "_id" : ObjectId("50e82ff4803d1a0d947eb66e"), "age" : 36, "name" : "William" }
> db.people.update( {}, {$set : {title : "Dr" } }, {multi : true});
> db.people.find()
{ "_id" : ObjectId("50e00064405fe32e05c6e6c0"), "age" : 24, "name" : "Wendyeq", "title" : "Dr" }
{ "_id" : ObjectId("50e001de405fe32e05c6e6c1"), "salary" : 50000, "title" : "Dr" }
{ "_id" : ObjectId("50e10b8ff5448477652e2365"), "age" : 35, "name" : 42, "title" : "Dr" }
{ "_id" : ObjectId("50e8fb790b336d073077842b"), "age" : 40, "name" : "George", "title" : "Dr" }
{ "_id" : ObjectId("50e82ff4803d1a0d947eb66e"), "age" : 36, "name" : "William", "title" : "Dr" }

删除

remove可以删除集合中的所有记录,drop删除集合结构,drop之后可以用show collections验证。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
> db.people.find()
{ "_id" : ObjectId("50e00064405fe32e05c6e6c0"), "age" : 24, "name" : "Wendyeq", "title" : "Dr" }
{ "_id" : ObjectId("50e001de405fe32e05c6e6c1"), "salary" : 50000, "title" : "Dr" }
{ "_id" : ObjectId("50e10b8ff5448477652e2365"), "age" : 35, "name" : 42, "title" : "Dr" }
{ "_id" : ObjectId("50e8fb790b336d073077842b"), "age" : 40, "name" : "George", "title" : "Dr" }
{ "_id" : ObjectId("50e82ff4803d1a0d947eb66e"), "age" : 36, "name" : "William", "title" : "Dr" }
> db.people.remove({name : "Wendyeq"})
> db.people.find()
{ "_id" : ObjectId("50e001de405fe32e05c6e6c1"), "salary" : 50000, "title" : "Dr" }
{ "_id" : ObjectId("50e10b8ff5448477652e2365"), "age" : 35, "name" : 42, "title" : "Dr" }
{ "_id" : ObjectId("50e8fb790b336d073077842b"), "age" : 40, "name" : "George", "title" : "Dr" }
{ "_id" : ObjectId("50e82ff4803d1a0d947eb66e"), "age" : 36, "name" : "William", "title" : "Dr" }
> db.people.remove({name : {$gt : "G"}})
> db.people.find()
{ "_id" : ObjectId("50e001de405fe32e05c6e6c1"), "salary" : 50000, "title" : "Dr" }
{ "_id" : ObjectId("50e10b8ff5448477652e2365"), "age" : 35, "name" : 42, "title" : "Dr" }
> db.people.remove()
> db.people.find()
> db.people.drop()
true

调试

可以使用getLastError来获取最后出错的日志。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
> db.people.insert({_id : "Smith", age : 30})
> db.people.insert({_id : "Smith", age : 30})
E11000 duplicate key error index: test.people.$_id_ dup key: { : "Smith" }
> db.runCommand({getLastError : 1})
{
"err" : "E11000 duplicate key error index: test.people.$_id_ dup key: { : \"Smith\" }",
"code" : 11000,
"n" : 0,
"connectionId" : 1,
"ok" : 1
}
> db.people.insert({_id : "Jones", age : 30})
> db.runCommand({getLastError : 1})
{ "n" : 0, "connectionId" : 1, "err" : null, "ok" : 1 }
> db.people.update({}, {$set: {title:"Dr"}}, {multi : true});
> db.runCommand({getLastError : 1})
{
"updatedExisting" : true,
"n" : 2,
"connectionId" : 1,
"err" : null,
"ok" : 1
}
> db.people.update({name : "Thompson"}, {$set: {title:"Dr"}}, {upsert : true});
> db.runCommand({getLastError : 1})
{
"updatedExisting" : false,
"upserted" : ObjectId("50e90f390b336d073077842c"),
"n" : 1,
"connectionId" : 1,
"err" : null,
"ok" : 1
}
> db.people.remove()
> db.runCommand({getLastError : 1})
{ "n" : 3, "connectionId" : 1, "err" : null, "ok" : 1 }