Operate on Mongodb 3.2

Server environment

1
2
3
4
5
$ mongo

MongoDB shell version v3.4.10
connecting to: mongodb://127.0.0.1:27017
MongoDB server version: 3.2.17

Insert Document

1
2
> db.blog.insert({"title":"mongo insert"})
WriteResult({ "nInserted" : 1 })

Batch Insert Document

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
> db.blog.insert([{"title":"mongo batch insert part 1"},{"title":"mongo batch insert part 2"},{"title":"mongo batch insert part 3"}])
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 3,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
> db.blog.find()
{ "_id" : ObjectId("5a202c00fb4c8a34cc1a4d81"), "title" : "mongo insert" }
{ "_id" : ObjectId("5a1fa36d4a00059e962af668"), "title" : "mongo batch insert part 1" }
{ "_id" : ObjectId("5a1fa36d4a00059e962af669"), "title" : "mongo batch insert part 2" }
{ "_id" : ObjectId("5a1fa36d4a00059e962af66a"), "title" : "mongo batch insert part 3" }

What about repeat insert?
The record {“_id” : ObjectId(“5a1fa36d4a00059e962af668”} is exist, i’m going to insert a repeat id record now.

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
> db.blog.insert([{"_id" : ObjectId("5a1fa36d4a00059e962af668"),"title":"mongo batch insert part 4"},{"title":"mongo batch insert part 5"}])
BulkWriteResult({
"writeErrors" : [
{
"index" : 0,
"code" : 11000,
"errmsg" : "E11000 duplicate key error collection: test.blog index: _id_ dup key: { : ObjectId('5a1fa36d4a00059e962af668') }",
"op" : {
"_id" : ObjectId("5a1fa36d4a00059e962af668"),
"title" : "mongo batch insert part 4"
}
}
],
"writeConcernErrors" : [ ],
"nInserted" : 0,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})
> db.blog.find()
{ "_id" : ObjectId("5a202c00fb4c8a34cc1a4d81"), "title" : "mongo insert" }
{ "_id" : ObjectId("5a1fa36d4a00059e962af668"), "title" : "mongo batch insert part 1" }
{ "_id" : ObjectId("5a1fa36d4a00059e962af669"), "title" : "mongo batch insert part 2" }
{ "_id" : ObjectId("5a1fa36d4a00059e962af66a"), "title" : "mongo batch insert part 3" }

Now we know the operate of batch insert will be break off if there is a duplicate key

  • After mongodb version 3.2, the method batchInsert is deprecated

InsertMany

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
> db.blog.insertMany([{"title":"mongo insertMany part 1"},{"title":"mongo insertMany part 2"},{"title":"mongo insertMany part 3"}])
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5a1faab4fb4c8a34cc1a4d7e"),
ObjectId("5a1faab4fb4c8a34cc1a4d7f"),
ObjectId("5a1faab4fb4c8a34cc1a4d80")
]
}
> db.blog.find()
{ "_id" : ObjectId("5a1fa79efb4c8a34cc1a4d73"), "title" : "mongo bulk insert - Ordered Operations part 1" }
{ "_id" : ObjectId("5a1fa79efb4c8a34cc1a4d74"), "title" : "mongo bulk insert - Ordered Operations part 2" }
{ "_id" : ObjectId("5a1fa79efb4c8a34cc1a4d75"), "title" : "mongo bulk insert - Ordered Operations part 3" }
{ "_id" : ObjectId("5a1faab4fb4c8a34cc1a4d7e"), "title" : "mongo insertMany part 1" }
{ "_id" : ObjectId("5a1faab4fb4c8a34cc1a4d7f"), "title" : "mongo insertMany part 2" }
{ "_id" : ObjectId("5a1faab4fb4c8a34cc1a4d80"), "title" : "mongo insertMany part 3" }

Bulk Insert

The two basic usage are Unordered Operations和Ordered Operations.

Ordered Operations, mongodb will stop and if one of the writing task list makes some errors when it is writing then other writing tasks wil not be continue.
Operations which already done will not be rollback.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
> var bulk = db.blog.initializeOrderedBulkOp();
> bulk.insert({"title":"mongo bulk insert - Ordered Operations part 1"});
> bulk.insert({"title":"mongo bulk insert - Ordered Operations part 2"});
> bulk.insert({"title":"mongo bulk insert - Ordered Operations part 3"});
> bulk.execute()
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 3,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"
> db.blog.find()
...
{ "_id" : ObjectId("5a1fa79efb4c8a34cc1a4d73"), "title" : "mongo bulk insert - Ordered Operations part 1" }
{ "_id" : ObjectId("5a1fa79efb4c8a34cc1a4d74"), "title" : "mongo bulk insert - Ordered Operations part 2" }
{ "_id" : ObjectId("5a1fa79efb4c8a34cc1a4d75"), "title" : "mongo bulk insert - Ordered Operations part 3" }

Unordered Operations, it will not be stop if one of the writing task list makes errors then other task will continue their work.So this usage is much faster

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
> var bulk = db.blog.initializeUnorderedBulkOp();
> bulk.insert({"title":"mongo bulk insert - Unordered Operations part 1"});
> bulk.insert({"title":"mongo bulk insert - Unordered Operations part 2"});
> bulk.insert({"title":"mongo bulk insert - Unordered Operations part 3"});
> bulk.execute();
BulkWriteResult({
"writeErrors" : [ ],
"writeConcernErrors" : [ ],
"nInserted" : 3,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})

> var bulk = db.blog.initializeUnorderedBulkOp();
> bulk.insert({"title":"mongo bulk insert - Unordered Operations part 4"});
> bulk.insert({"_id" : ObjectId("5a1fa81efb4c8a34cc1a4d79"),"title":"mongo bulk insert - Unordered Operations part 5"});
> bulk.insert({"title":"mongo bulk insert - Unordered Operations part 6"});
> bulk.execute();
2017-03-22T14:43:51.928+0800 E QUERY [thread1] BulkWriteError: write error at item 1 in bulk operation :
BulkWriteError({
"writeErrors" : [
{
"index" : 1,
"code" : 11000,
"errmsg" : "E11000 duplicate key error collection: test.blog index: _id_ dup key: { : ObjectId('5a1fa81efb4c8a34cc1a4d79') }",
"op" : {
"_id" : ObjectId("5a1fa81efb4c8a34cc1a4d79"),
"title" : "mongo bulk insert - Unordered Operations part 5"
}
}
],
"writeConcernErrors" : [ ],
"nInserted" : 2,
"nUpserted" : 0,
"nMatched" : 0,
"nModified" : 0,
"nRemoved" : 0,
"upserted" : [ ]
})

> db.blog.find()
...
{ "_id" : ObjectId("5a1fa81efb4c8a34cc1a4d79"), "title" : "mongo bulk insert - Unordered Operations part 1" }
{ "_id" : ObjectId("5a1fa81efb4c8a34cc1a4d7a"), "title" : "mongo bulk insert - Unordered Operations part 2" }
{ "_id" : ObjectId("5a1fa81efb4c8a34cc1a4d7b"), "title" : "mongo bulk insert - Unordered Operations part 3" }
{ "_id" : ObjectId("5a1fa8a7fb4c8a34cc1a4d7c"), "title" : "mongo bulk insert - Unordered Operations part 4" }
{ "_id" : ObjectId("5a1fa8a7fb4c8a34cc1a4d7d"), "title" : "mongo bulk insert - Unordered Operations part 6" }
  • The max document size must less than 16MB.

Update

1
2
3
4
5
6
7
8
9
10
11
12
13
> db.blog.update({"_id" : ObjectId("5a202c00fb4c8a34cc1a4d81")},{"$set":{"subTitle":"mongo update","tags":["mongo","mongo3.2","mongo update"]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.blog.findOne({"_id" : ObjectId("5a202c00fb4c8a34cc1a4d81")})
{
"_id" : ObjectId("5a202c00fb4c8a34cc1a4d81"),
"title" : "mongo insert",
"subTitle" : "mongo update",
"tags" : [
"mongo",
"mongo3.2",
"mongo update"
]
}

Append Update

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.blog.update({"_id" : ObjectId("5a202c00fb4c8a34cc1a4d81")},{"$push":{"comments":[{"name":"liang","content":"wow~ lucky!"},{"name":"Jack","content":"hihi~~"}]}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.blog.findOne({"_id" : ObjectId("5a202c00fb4c8a34cc1a4d81")})
{
"_id" : ObjectId("5a202c00fb4c8a34cc1a4d81"),
"title" : "mongo insert",
"subTitle" : "mongo update",
"tags" : [
"mongo",
"mongo3.2",
"mongo update"
],
"comments" : [
[
{
"name" : "liang",
"content" : "wow~ lucky!"
},
{
"name" : "Jack",
"content" : "hihi~~"
}
]
]
}

Update record does not exist

1
2
> db.blog.update({"_id" : "notexistid"},{"$set":{"content":"not exist record"}},true)
WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0 })

Upsert

1
2
3
4
5
> db.blog.update({"_id" : "notexistid"},{"$set":{"content":"not exist record"}},true)
WriteResult({ "nMatched" : 0, "nUpserted" : 1, "nModified" : 0, "_id" : "notexistid" })
> db.blog.find()
...
{ "_id" : "notexistid", "content" : "not exist record" }
  • The third parameter in update method indicates that if the record does not exist, the operation will become an insert operation

Multi Update

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
> db.blog.update({"title":{"$ne":""}},{"$set":{"author":"liang"}},false,true)
WriteResult({ "nMatched" : 13, "nUpserted" : 0, "nModified" : 13 })
> db.blog.find()
{ "_id" : ObjectId("5a1fa79efb4c8a34cc1a4d73"), "title" : "mongo bulk insert - Ordered Operations part 1", "author" : "liang" }
{ "_id" : ObjectId("5a1fa79efb4c8a34cc1a4d74"), "title" : "mongo bulk insert - Ordered Operations part 2", "author" : "liang" }
{ "_id" : ObjectId("5a1fa79efb4c8a34cc1a4d75"), "title" : "mongo bulk insert - Ordered Operations part 3", "author" : "liang" }
{ "_id" : ObjectId("5a1fa81efb4c8a34cc1a4d79"), "title" : "mongo bulk insert - Unordered Operations part 1", "author" : "liang" }
{ "_id" : ObjectId("5a1fa81efb4c8a34cc1a4d7a"), "title" : "mongo bulk insert - Unordered Operations part 2", "author" : "liang" }
{ "_id" : ObjectId("5a1fa81efb4c8a34cc1a4d7b"), "title" : "mongo bulk insert - Unordered Operations part 3", "author" : "liang" }
{ "_id" : ObjectId("5a1fa8a7fb4c8a34cc1a4d7c"), "title" : "mongo bulk insert - Unordered Operations part 4", "author" : "liang" }
{ "_id" : ObjectId("5a1fa8a7fb4c8a34cc1a4d7d"), "title" : "mongo bulk insert - Unordered Operations part 6", "author" : "liang" }
{ "_id" : ObjectId("5a1faab4fb4c8a34cc1a4d7e"), "title" : "mongo insertMany part 1", "author" : "liang" }
{ "_id" : ObjectId("5a1faab4fb4c8a34cc1a4d7f"), "title" : "mongo insertMany part 2", "author" : "liang" }
{ "_id" : ObjectId("5a1faab4fb4c8a34cc1a4d80"), "title" : "mongo insertMany part 3", "author" : "liang" }
{ "_id" : ObjectId("5a202c00fb4c8a34cc1a4d81"), "title" : "mongo insert", "subTitle" : "mongo update", "tags" : [ "mongo", "mongo3.2", "mongo update" ], "comments" : [ [ { "name" : "liang", "content" : "wow~ lucky!" }, { "name" : "Jack", "content" : "hihi~~" } ] ], "author" : "liang" }
{ "_id" : "notexistid", "content" : "not exist record", "author" : "liang" }
  • The fourth parameter in update method indicates that all matching documents will be updated

findAndModify

Note that the findAndModify operation is atomic !

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
> db.runCommand({"findAndModify":"blog"
... ,"query":{"_id" : ObjectId("5a202c00fb4c8a34cc1a4d81")},
... "sort":{"_id":"-1"},
... "update":{"$set":{"price":100}}
... })
{
"lastErrorObject" : {
"updatedExisting" : true,
"n" : 1
},
"value" : {
"_id" : ObjectId("5a202c00fb4c8a34cc1a4d81"),
"title" : "mongo insert",
"subTitle" : "mongo update",
"tags" : [
"mongo",
"mongo3.2",
"mongo update"
],
"comments" : [
[
{
"name" : "liang",
"content" : "wow~ lucky!"
},
{
"name" : "Jack",
"content" : "hihi~~"
}
]
],
"author" : "liang"
},
"ok" : 1
}

Find

Insert a record.

1
2
> db.blog.insert({"title":"mongo find","author":"liang","language":"english","price":{"max":99,"min":60}})
WriteResult({ "nInserted" : 1 })

1
2
> db.blog.find({"price.max":{"$lte":99,"$gte":60},"author":"liang"},{"title":1,"author":1,"_id":0})
{ "title" : "mongo find", "author" : "liang" }

The first param is query condition, the second param is query result(it will not return if this value is zero)

Cursor

1
2
3
4
5
6
7
8
9
> for(i=0;i<100;i++){
... db.blog.insert({"author":"liang","title":"cursor insert "+i});
... }
WriteResult({ "nInserted" : 1 })

> var cursor = db.blog.find();
> while(cursor.hasNext()){
... obj = cursor.next();
... }
  • If the number of query result is huge, cursor will meet a repeat document
    If document becomes bigger when data from database increased after business process, this document ‘s memory address will be reassign.
    When the cursor is moved to right, it will re-meet the same document with a new address again
Share