MongoDB Special Instructions Usage

Source: Internet
Author: User
Tags mongodb update

http://apluck.iteye.com/blog/1176160 A. Modify the Collection collection field name

See also: Http://www.mongodb.org/display/DOCS/Updating#Updating-%24rename

Cases:

Shell CodeDb.test.find ()

Find the following data:

Shell Code{"_id": ObjectId ("4e787a369748ed04b7c19b0f"), "abc": "123"}

Now, you need to change the field "ABC" to "Def" and execute the following command:

Shell Codedb.test.update ({}, {$rename: {"abc": "Def"}}, False, True)

Can.

PS: If the amount of data is large, the column name of all data will not be renamed immediately, the execution time is required.

B. In queryShell CodeDb.col.find ({"_id": {$in: [1,2,3,4 ...]})[A.] Array. MongoDB Update has two commands:
1). Update () command

Db.collection.update (criteria, objnew, Upsert, Multi)

Criteria:update query conditions, similar to those in the SQL update query
Objnew:update objects and some updated operators (such as $, $inc ... ) can also be understood as the SQL update query within the set after the
Upsert: This parameter means that if there is no record of update, insert Objnew,true is inserted, default is False, not inserted.
Multi:mongodb default is False, only update the first record found, if this parameter is true, the condition is checked out all the records are updated.

Cases:
Db.test0.update ({"Count": {$gt: 1}}, {$set: {"test2": "OK"}}); Only the first record is updated
Db.test0.update ({"Count": {$gt: 3}}, {$set: {"test2": "OK"}},false,true); It's all updated.
Db.test0.update ({"Count": {$gt: 4}}, {$set: {"Test5": "OK"}},true,false); Only added the first one.
Db.test0.update ({"Count": {$gt: 5}}, {$set: {"Test5": "OK"}},true,true); All added in.
Db.test0.update ({"Count": {$gt: $}}, {$inc: {"Count": 1}},false,true), fully updated
Db.test0.update ({"Count": {$gt: Ten}}, {$inc: {"Count": 1}},false,false); Update only the first one

2). Save () command

Db.collection.save (x)

X is the object to be updated, only a single record.

If a record of the same "_id" as the X object already exists within the collection. MongoDB will replace the X object with the existing record in collection, otherwise the X object will be inserted, and if there is no _id within X, a re-insert will be generated automatically. Equivalent to the upsert=true,multi=false of the above UPDATE statement.

Cases:
Db.test0.save ({count:40,test1: "OK"}); #_id系统会生成
Db.test0.save ({_id:40,count:40,test1: "OK"}); #如果test0内有_id等于40的, will be replaced, otherwise inserted.


The update operator for MongoDB:

1) $inc

Usage: {$inc: {Field:value}}

Meaning to a Number field field adds value, for example:

> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": +}, "Count": +, "test1": "Testtest", "test2": "OK", "test3": "Testtest", "test4": "OK" , "Test5": "OK"}

> db.test0.update ({"_id":}, {$inc: {"Count": 1}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": {"}", "Count": +, "test1": "Testtest", "test2": "OK", "test3": "Testtest", "test4": "OK" , "Test5": "OK"}

> db.test0.update ({"_id":}, {$inc: {"Count": 2}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox":}, "Count": +, "test1": "Testtest", "test2": "OK", "test3": "Testtest", "test4": "OK" , "Test5": "OK"}

> db.test0.update ({"_id":}, {$inc: {"Count":-1}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": {"}", "Count": "Test1": "Testtest", "test2": "OK", "test3": "Testtest", "test4": "OK" , "Test5": "OK"}


2) $set

Usage: {$set: {Field:value}}

is the equivalent of SQL set field = value, all data types support $set. Cases:
> db.test0.update ({"_id":}, {$set: {"test1": "TESTV1", "test2": "Testv2", "test3": "TESTV3", "test4": "Test V4 "}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "Count": "Test1": "TESTV1", "test2": "Testv2", "test3": "TESTV3", "test4": "tes TV4 "," Test5 ":" OK "}

3) $unset

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

As the name implies, the field is deleted. Cases:
> db.test0.update ({"_id":}, {$unset: {"test1": 1}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": {"}", "Count":, "test2": "Testv2", "test3": "TESTV3", "test4": "TESTV4", "Test5": "OK" }

> db.test0.update ({"_id":}, {$unset: {"test2": 0}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "Count": "Test3": "TESTV3", "test4": "TESTV4", "Test5": "OK"}

> db.test0.update ({"_id":}, {$unset: {"test3": asdfasf}});
Fri 16:17:38 JS Error:ReferenceError:asdfasf is not defined (shell): 0

> db.test0.update ({"_id":}, {$unset: {"test3": "Test"}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": +}, "Count": "Test4": "TESTV4", "Test5": "OK"}

Do not see field:1 inside of 1 is what to use, anyway as long as there is something on the line.


4) $push
Usage: {$push: {Field:value}}

Append value To field, field must be array type, if field does not exist, a new array type will be added. Cases:

> db.test0.update ({"_id":}, {$set: {"test1": ["AAA", "BBB"]}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "Count":, "test1": ["AAA", "BBB"], "test4": "TESTV4", "Test5": "OK"}

> db.test0.update ({"_id":}, {$push: {"test1": "CCC"}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": {"}", "Count":, "test1": ["AAA", "BBB", "CCC"], "test4": "TESTV4", "Test5": "OK"}

> db.test0.update ({"_id":}, {$push: {"test2": "CCC"}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "Count":, "test1": ["AAA", "BBB", "CCC"], "test2": ["CCC"], "test4": "TESTV4" , "Test5": "OK"}

> db.test0.update ({"_id":}, {$push: {"test1": ["ddd", "Eee"]}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "count": +, "test1": ["AAA", "BBB", "CCC", ["ddd", "Eee"]], "test2": ["CCC"], "Test4": "TESTV4", "Test5": "OK"} 5) $pushAll

Usage: {$pushAll: {Field:value_array}}

With $push, it is possible to append multiple values to an array field at a time. Cases:

> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "count": +, "test1": ["AAA", "BBB", "CCC", ["ddd", "Eee"]], "test2": ["CCC"], "Test4": "TESTV4", "Test5": "OK"}

> db.test0.update ({"_id":}, {$pushAll: {"test1": ["fff", "GGG"]});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "count": +, "test1": ["AAA", "BBB", "CCC", ["ddd", "Eee"], "fff", "GGG"], "test2" : ["CCC"], "test4": "TESTV4", "Test5": "OK"}

6) $addToSet
Usage: {$addToSet: {field:value}}

Can remove duplicate columns

> db.members.update ({' name ': ' Hurry '},{' $addToSet ': {' skills ': ' Python '}})

> Db.members.find ()

{"_id": ObjectId ("4e11b35ec25a7683c014d96a"), "age": +, "name": "Hurry", "Skills": ["Java", "JS", "Python"], "titl E ":" Software Developer "}

$each add more than one at a time

> db.members.update ({' name ': ' Hurry '},{' $addToSet ': {' skills ': {$each: [' python ', ' mongoDB ', ' PL/sql ']}})

> Db.members.find ()

{"_id": ObjectId ("4e11b35ec25a7683c014d96a"), "age": +, "name": "Hurry", "Skills": ["Java", "JS", "python", "PL/SQL "," MongoDB "]," title ":" Software Developer "}


Adds a value to the array, and only if the value is not inside the array. Cases:
> db.test0.update ({"_id":}, {$addToSet: {"test1": {$each: ["444", "555"]}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "Count": "Test1": [
"AAA",
"BBB",
"CCC",
[
"DDD",
"Eee"
],
"FFF",
"GGG",
[
"111",
"222"
],
"444",
"555"
], "test2": ["CCC"], "test4": "TESTV4", "Test5": "OK"}
> db.test0.update ({"_id":}, {$addToSet: {"test1": {$each: ["444", "555"]}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "Count": "Test1": [
"AAA",
"BBB",
"CCC",
[
"DDD",
"Eee"
],
"FFF",
"GGG",
[
"111",
"222"
],
"444",
"555"
], "test2": ["CCC"], "test4": "TESTV4", "Test5": "OK"}
> db.test0.update ({"_id":}, {$addToSet: {"test1": ["444", "555"]}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "Count": "Test1": [
"AAA",
"BBB",
"CCC",
[
"DDD",
"Eee"
],
"FFF",
"GGG",
[
"111",
"222"
],
"444",
"555",
[
"444",
"555"
]
], "test2": ["CCC"], "test4": "TESTV4", "Test5": "OK"}
> db.test0.update ({"_id":}, {$addToSet: {"test1": ["444", "555"]}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "Count": "Test1": [
"AAA",
"BBB",
"CCC",
[
"DDD",
"Eee"
],
"FFF",
"GGG",
[
"111",
"222"
],
"444",
"555",
[
"444",
"555"
]
], "test2": ["CCC"], "test4": "TESTV4", "Test5": "OK"}


7) $pop

To delete a value within an array

Usage:
Delete last value: {$pop: {field:1}}
Delete First value: {$pop: {field:-1}}

Note that only one value can be deleted, that is, only 1 or-1, not 2 or--and two are deleted. MongoDB 1.1 and later versions are only available, for example:
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "Count": "Test1": [
"BBB",
"CCC",
[
"DDD",
"Eee"
],
"FFF",
"GGG",
[
"111",
"222"
],
"444"
], "test2": ["CCC"], "test4": "TESTV4", "Test5": "OK"}
> db.test0.update ({"_id":}, {$pop: {"Test1":-1}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "Count": "Test1": [
"CCC",
[
"DDD",
"Eee"
],
"FFF",
"GGG",
[
"111",
"222"
],
"444"
], "test2": ["CCC"], "test4": "TESTV4", "Test5": "OK"}
> db.test0.update ({"_id":}, {$pop: {"test1": 1}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": {"}", "Count": "Test1": ["CCC", ["ddd", "Eee"], "fff", "GGG", ["111", "222"], "TE St2 ": [" CCC "]," test4 ":" TESTV4 ",
"Test5": "OK"}

8) $pull

Usage: $pull: {Field:value}}

Deletes an equal value from the array field. Cases:
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": {"}", "Count": "Test1": ["CCC", ["ddd", "Eee"], "fff", "GGG", ["111", "222"], "TE St2 ": [" CCC "]," test4 ":" TESTV4 ",
"Test5": "OK"}

> db.test0.update ({"_id":}, {$pull: {"test1": "GGG"}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "Count": "Test1": ["CCC", ["ddd", "Eee"], "fff", ["111", "222"]], "test2": ["CCC"], "test4": "TESTV4", "Test5"
: "OK"}

9) $pullAll

Usage: {$pullAll: {Field:value_array}}

With $pull, you can delete multiple values within an array at a time. Cases:
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "Count": "Test1": ["CCC", ["ddd", "Eee"], "fff", ["111", "222"]], "test2": ["CCC"], "test4": "TESTV4", "Test5"
: "OK"}

> db.test0.update ({"_id":}, {$pullAll: {"test1": ["CCC", "FFF"]}});
> Db.test0.find ({"_id": 15});
{"_id": {"Floatapprox": "$", "Count": "Test1": [["ddd", "Eee"], ["111", "222"]], "test2": ["CCC"], "TE St4 ":" TESTV4 "," Test5 ":" OK "}


10) $ operator

$ is his own meaning, represented by the condition of finding an array inside an item of his own. Oh, compare the AU kou. 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 is important to note that $ only applies the first array item found, followed by no matter. Or look at an 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 used with $unset, a null array entry is left, but you can delete all null array items with {$pull: {x:null}}. Cases:
> 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]}

MongoDB Special Instructions Usage

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.