1024programmer Mongodb Detailed explanation of the update method of MongoDB database

Detailed explanation of the update method of MongoDB database

In the previous article “mongodb
In “Query Syntax”, I introduced the common query syntax of Mongodb. The update operation of Mongodb is also a bit complicated. I will introduce it here based on my own experience and show it to friends who use mongodb. It is also convenient to refer to when you use it later:

Note: The grammar introduction in this article and the previous article are all in mongodb
In the shell environment, there are some differences in usage methods when using real language programming (such as java, php, etc.), but the syntax (such as query conditions, $in, $inc, etc.) is the same.

This article is introduced with reference to the official documents. The reason why there are official documents is introduced here. On the one hand, it is used as a translation. It’s hard to understand from the official documentation, I can make some supplements in actual operation.

Well, without further ado, let’s start:

There are two commands for mongodb update:

1).update() command

db.collection.update( criteria, objNew, upsert, multi )

criteria : query criteria for update, similar to the
behind where in the sql update query
objNew : update object and some update operators (such as $, $inc…), etc., can also be understood as sql
The
behind the set in the update query
upsert :
This parameter means, if there is no update record, whether to insert objNew, true is inserted, the default is false, no insertion.
multi :
mongodb is false by default, and only the first record found is updated. If this parameter is true, all the multiple records detected according to the condition will be updated.

Example:
db.test0.update( { “count” : { $gt : 1 } } , { $set : { “test2” :
“OK”} } ); Only the first record has been updated
db.test0.update( { “count” : { $gt : 3 } } , { $set : { “test2” :
“OK”} },false,true ); Fully updated
db.test0.update( { “count” : { $gt : 4 } } , { $set : { “test5” :
“OK”} },true,false ); Only the first item is added
db.test0.update( { “count” : { $gt : 5 } } , { $set : { “test5” :
“OK”} },true,true ); all added
db.test0.update( { “count” : { $gt : 15 } } , { $inc : { “count” :
1} },false,true ); all updated
db.test0.update( { “count” : { $gt : 10 } } , { $inc : { “count” :
1} },false,false ); Only the first item is updated

2).save() command

db. collection. save( x )

x is the object to be updated, which can only be a single record.

If there is already a record with the same “_id” as the x object in the collection. mongodb will replace the x object with the existing record in the collection, otherwise it will insert the x object, if there is no _id in x, the system will automatically generate one and then insert it. It is equivalent to the case of upsert=true and multi=false of the update statement above.

Example:
db.test0.save({count:40,test1:”OK”}); #_id will be generated by the system
db.test0.save({_id:40,count:40,test1:”OK”});
#If there is an _id equal to 40 in test0, it will be replaced, otherwise it will be inserted.

The update operator of mongodb:

1) $inc

Usage: { $inc : { field : value } }

It means adding value to a numeric field field, for example:

> db.test0.find( { “_id” : 15 } );
{ “_id” : { “floatApprox” : 15 }, “count” : 16, “test1” :
“TESTTEST”, “test2” : “OK”, “test3” : “TESTTEST”, “test4” : “OK”,
“test5” : “OK” }

> db.test0.update( { “_id” : 15 } , { $inc : { “count” : 1 }
} );
> db.test0.find( { “_id” : 15 } );
{ “_id” : { “floatApprox” : 15 }, “count” : 17, “test1” :
“TESTTEST”, “test2” : “OK”, “test3” : “TESTTEST”, “test4” : “OK”,
“test5” : “OK” }

> db.test0.update( { “_id” : 15 } , { $inc : { “count” : 2 }
} );
> db.test0.find( { “_id” : 15 } );
{ “_id” : { “floatApprox” : 15 }, “count” : 19, “test1” :
“TESTTEST”, “test2” : “OK”, “test3” : “TESTTEST”, “test4” : “OK”,
“test5” : “OK” }

> db.test0.update( { “_id” : 15 } , { $inc : { “count” : -1 }
} );
> db.test0.find( { “_id” : 15 } );
{ “_id” : { “floatApprox” : 15 }, “count” : 18, “test1” :
“TESTTEST”, “test2” : “OK”, “test3” : “TESTTEST”, “test4” : “OK”,
“test5” : “OK” }

2) $set

Usage: { $set : { field : value } }

It is equivalent to sql’s set field = value, and all data types support $set. Example:
> db.test0.update( { “_id” : 15 } , { $set : { “test1” :
“testv1″,”test2”: “testv2″,”test3”: “testv3″,”test4″:”testv4”}
} );
> db.test0.find( { “_id” : 15 } );
{ “_id” : { “floatApprox” : 15 }, “count” : 18, “test1” : “testv1”,
“test2” : “testv2”, “test3” : “testv3”, “test4” : “testv4”, “test5”
: “OK” }

3) $unset

Usage: { $unset : { field : 1} }

As the name suggests, it is to delete the field. Example:
> db.test0.update( { “_id” : 15 } , { $unset : { “test1”:1 } }
);
> db.test0.find( { “_id” : 15 } );
{ “_id” : { “floatApprox” : 15 }, “count” : 18, “test2” : “testv2”,
“test3” : “testv3”, “test4” : “testv4”, “test5″ :;”count” : 18, “test1” : [
“aaa”,
“bbb”,
“ccc”,
[
“ddd”,
“eee”
],
“fff”,
“ggg”,
[
“111”,
“222”
],
“444”,
“555”
], “test2” : [ “ccc” ], “test4” : “testv4”, “test5” : “OK” }
> db.test0.update( { “_id” : 15 } , { $addToSet : { “test1”: [“444″,”555”] } } );
> db.test0.find( { “_id” : 15 } );
{ “_id” : { “floatApprox” : 15 }, “count” : 18, “test1” : [
“aaa”,
“bbb”,
“ccc”,
[
“ddd”,
“eee”
],
“fff”,
“ggg”,
[
“111”,
“222”
],
“444”,
“555”,
[
“444”,
“555”
]
], “test2” : [ “ccc” ], “test4” : “testv4”, “test5” : “OK” }
> db.test0.update( { “_id” : 15 } , { $addToSet : { “test1”: [“444″,”555”] } } );
> db.test0.find( { “_id” : 15 } );
{ “_id” : { “floatApprox” : 15 }, “count” : 18, “test1” : [
“aaa”,
“bbb”,
“ccc”,
[
“ddd”,
“eee”
],
“fff”,
“ggg”,
[
“111”,
“222”
],
“444”,
“555”,
[
“444”,
“555”
]
], “test2” : [ “ccc” ], “test4” : “testv4”, “test5” : “OK” }
7) $pop
Delete a value in the array
Usage:
Delete last value: { $pop : { field : 1 } }
Delete the first value: { $pop : { field : -1 } }
Note that only one value can be deleted, that is to say, only 1 or -1 can be used, but 2 or -2 cannot be used to delete two. Only mongodb 1.1 and later versions can be used, for example:
> db.test0.find( { “_id” : 15 } );
{ “_id” : { “floatApprox” : 15 }, “count” : 18, “test1” : [
“bbb”,
“ccc”,
[
“ddd”,
“eee”
],
“fff”,
“ggg”,
[
“111”,
“222”
],
“444”
], “test2” : [ “ccc” ], “test4” : “testv4”, “test5” : “OK” }
> db.test0.update( { “_id” : 15 } , { $pop : { “test1”: -1 } } );
> db.test0.find( { “_id” : 15 } );
{ “_id” : { “floatApprox” : 15 }, “count” : 18, “test1” : [
“ccc”,
[
“ddd”,
“eee”
],
“fff”,
“ggg”,
[
“111”,
“222”
],
“444”
], “test2” : [ “ccc” ], “test4” : “testv4”, “test5” : “OK” }
> db.test0.update( { “_id” : 15 } , { $pop : { “test1”: 1 } } );
> db.test0.find( { “_id” : 15 } );
{ “_id” : { “floatApprox” : 15 }, “count” : 18, “test1” : [ “ccc”, [ “ddd”, “eee” ], “fff”, “ggg”, [ “111” , “222” ] ], “test2” : [ “ccc” ], “test4” : “testv4”,
“test5” : “OK” }
8) $pull
Usage: $pull : { field : value } }
Deletes a value equal to value from the array field. Example:
> db.test0.find( { “_id” : 15 } );
{ “_id” : { “floatApprox” : 15 }, “count” : 18, “test1” : [ “ccc”, [ “ddd”, “eee” ], “fff”, “ggg”, [ “111” , “222” ] ], “test2” : [ “ccc” ], “test4” : “testv4”,
“test5” : “OK” }
> db.test0.update( { “_id” : 15 } , { $pull : { “test1”: “ggg” } } );
> db.test0.find( { “_id” : 15 } );
{ “_id” : { “floatApprox” : 15 }, “count” : 18, “test1” : [ “ccc”, [ “ddd”, “eee” ], “fff”, [ “111”, “222” ] ], “test2” : [ “ccc” ], “test4” : “testv4”, “test5”
: “OK” }
9) $pullAll
Usage: { $pullAll : { field : value_array } }
Same as $pull, you can delete multiple values ​​in the array at one time. Example:
> db.test0.find( { “_id” : 15 } );
{ “_id” : { “floatApprox” : 15 }, “count” : 18, “test1” : [ “ccc”, [ “ddd”, “eee” ], “fff”, [ “111”, “222” ] ], “test2” : [ “ccc” ], “test4” : “testv4”, “test5”
: “OK” }
> db.test0.update( { “_id” : 15 } , { $pullAll : { “test1”: [ “ccc”, “fff” ] } } );
> db.test0.find( { “_id” : 15 } );
{ “_id” : { “floatApprox” : 15 }, “count” : 18, “test1” : [ [ “ddd”, “eee” ], [ “111”, “222” ] ], “test2” : [ “ccc” ], “test4” : “testv4”, “test5” : “OK” }
10) $ operator

$ is his own meaning, representing himself in a certain item in the array found by the condition. Hehe, it’s more like a mouth. Take a look at the official example:
> t. find()
{ “_id” : ObjectId(“4b97e62bf1d8c7152c9ccb74”), “title” : “ABC”, “comments” : [ { “by” : “joe”, “votes” : 3 }, { “by” : “jane”, “votes” : 7 } ] }
> t.update( {‘comments.by’:’joe’}, {$inc:{‘comments.$.votes’:1}}, false, true )
> t. find()
{ “_id” : ObjectId(“4b97e62bf1d8c7152c9ccb74”), “title” : “ABC”, “comments” : [ { “by” : “joe”, “votes” : 4 }, { “by” : “jane”, “votes” : 7 } ] }
It should be noted that $ will only apply the first array item found, and the rest will be ignored. Still look at the example:
> t.find();
{ “_id” : ObjectId(“4b9e4a1fc583fa1c76198319”), “x” : [ 1, 2, 3, 2 ] }
> t.update({x: 2}, {$inc: {“x.$”: 1}}, false, true);
> t.find();
Also note that when $ is used with $unset, a null array item will be left, but {$pull:{x:null}} can be used to delete all null array items. Example:
> t.insert({x: [1,2,3,4,3,2,3,4]})
> t. find()
{ “_id” : ObjectId(“4bde2ad3755d00000000710e”), “x” : [ 1, 2, 3, 4, 3, 2, 3, 4 ] }
> t.update({x:3}, {$unset:{“x.$”:1}})
> t. find()
{ “_id” : ObjectId(“4bde2ad3755d00000000710e”), “x” : [ 1, 2, null, 4, 3, 2, 3, 4 ] }
{ “_id” : ObjectId(“4b9e4a1fc583fa1c76198319”), “x” : [ 1, 3, 3, 2 ] }

sp;”111″, “222” ] ], “test2” : [ “ccc” ], “test4” : “testv4”, “test5”
: “OK” }
9) $pullAll
Usage: { $pullAll : { field : value_array } }
Same as $pull, you can delete multiple values ​​in the array at one time. Example:
> db.test0.find( { “_id” : 15 } );
{ “_id” : { “floatApprox” : 15 }, “count” : 18, “test1” : [ “ccc”, [ “ddd”, “eee” ], “fff”, [ “111”, “222” ] ], “test2” : [ “ccc” ], “test4” : “testv4”, “test5”
: “OK” }
> db.test0.update( { “_id” : 15 } , { $pullAll : { “test1”: [ “ccc”, “fff” ] } } );
> db.test0.find( { “_id” : 15 } );
{ “_id” : { “floatApprox” : 15 }, “count” : 18, “test1” : [ [ “ddd”, “eee” ], [ “111”, “222” ] ], “test2” : [ “ccc” ], “test4” : “testv4”, “test5” : “OK” }
10) $ operator

$ is his own meaning, representing himself in a certain item in the array found by the condition. Hehe, it’s more like a mouth. Take a look at the official example:
> t. find()
{ “_id” : ObjectId(“4b97e62bf1d8c7152c9ccb74”), “title” : “ABC”, “comments” : [ { “by” : “joe”, “votes” : 3 }, { “by” : “jane”, “votes” : 7 } ] }
> t.update( {‘comments.by’:’joe’}, {$inc:{‘comments.$.votes’:1}}, false, true )
> t. find()
{ “_id” : ObjectId(“4b97e62bf1d8c7152c9ccb74”), “title” : “ABC”, “comments” : [ { “by” : “joe”, “votes” : 4 }, { “by” : “jane”, “votes” : 7 } ] }
It should be noted that $ will only apply the first array item found, and the rest will be ignored. Still look at the example:
> t.find();
{ “_id” : ObjectId(“4b9e4a1fc583fa1c76198319”), “x” : [ 1, 2, 3, 2 ] }
> t.update({x: 2}, {$inc: {“x.$”: 1}}, false, true);
> t.find();
Also note that when $ is used with $unset, a null array item will be left, but {$pull:{x:null}} can be used to delete all null array items. Example:
> t.insert({x: [1,2,3,4,3,2,3,4]})
> t. find()
{ “_id” : ObjectId(“4bde2ad3755d00000000710e”), “x” : [ 1, 2, 3, 4, 3, 2, 3, 4 ] }
> t.update({x:3}, {$unset:{“x.$”:1}})
> t. find()
{ “_id” : ObjectId(“4bde2ad3755d00000000710e”), “x” : [ 1, 2, null, 4, 3, 2, 3, 4 ] }
{ “_id” : ObjectId(“4b9e4a1fc583fa1c76198319”), “x” : [ 1, 3, 3, 2 ] }

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/detailed-explanation-of-the-update-method-of-mongodb-database/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索