Mongo dB updata

Source: Internet
Author: User
Tags mongodb query mongodb update
Http://www.mongodb.org/display/docs/advanced?queries=advancedqueries-conditionaloperators:<,<=, >,>=

MongoDB update data syntax

In the previous article "MongoDB query syntax", I introduced the common query syntax of MongoDB, and the update operation of MongoDB is a bit complicated. I will introduce it here based on my experience, let's take a look at MongoDB users. It is also convenient for future users to refer:

Note: The syntax introduced in this article and the previous article is in the mongodbshell environment and used in real language programming (such as Java and PHP, there are some differences in usage, but the syntax (such as query conditions, $ in, $ Inc, etc.) is the same.

This article is based on the official documentation. The reason why there is an official document is to introduce it here. On the one hand, it is a translation. After all, it is quite tiring to read the English document every time you need it, the second is that the official documentation is relatively simple to explain. Sometimes it is hard to understand the official documentation. I can make some supplements in actual operations.

Well, let's not talk about it. The following is the official start:

MongoDB update has two commands:

1). Update () command

DB. collection. Update (criteria, objnew, upsert, multi)

Criteria: Query condition for update, similar to
Objnew : Update objects and some updated operators (such as $, $ Inc...) can also be understood
Upsert : This parameter indicates whether to insert objnew if the update record does not exist. True indicates insertion. The default value is false.
Multi  : The default value of MongoDB is false. Only the first record found is updated. If this parameter is set to true, all records identified by the condition are updated.

Example:
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); all updated
DB. test0.update ({"count": {$ GT: 4 }},{ $ set: {"test5": "OK" }}, true, false); only the first entry 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 entry is updated.

2). Save () command

DB. collection. Save (X)

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

If a record with the same "_ id" as the X object already exists in the collection object. MongoDB will replace the X object with the existing records in the collection. Otherwise, the X object will be inserted. If there is no _ id in X, the system will automatically generate one and insert it again. This is equivalent to the above update statement upsert = true, multi = false.

Example:
DB. test0.save ({count: 40, test1: "OK"}); # _ ID system will generate
DB. test0.save ({_ ID: 40, Count: 40, test1: "OK"}); # If _ id equals 40 exists in test0, It will be replaced; otherwise, it will be inserted.

MongoDB update OPERATOR:

1) $ Inc

Usage: {$ Inc: {field: Value }}

Add value to a numeric 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. 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, fields are deleted. 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": "OK "}

> DB. test0.update ({"_ id": 15 },{$ unset: {"Test2": 0 }});
> DB. test0.find ({"_ id": 15 });
{"_ Id": {"floatapprox": 15}, "Count": 18, "test3": "testv3", "test4": "testv4", "test5 ": "OK "}

> DB. test0.update ({"_ id": 15 },{$ unset: {"test3": asdfasf }});
Fri May 14 16:17:38 JS error: referenceerror: asdfasf is notdefined (Shell): 0

> DB. test0.update ({"_ id": 15 },{$ unset: {"test3": "test "}});
> DB. test0.find ({"_ id": 15 });
{"_ Id": {"floatapprox": 15}, "Count": 18, "test4": "testv4", "test5": "OK "}

I don't know what field: 1 is used for. Everything is needed.

4) $ push

Usage: {$ push: {field: Value} adds the value to the field. The field must be of the array type. If the field does not exist, an array type is added. Example:> dB. test0.update ({"_ id": 15 },{$ set: {"test1": ["AAA", "BBB"]});> dB. test0.find ({"_ id": 15}); {"_ id": {"floatapprox": 15}, "Count": 18, "test1 ": ["AAA", "BBB"], "test4": "testv4", "test5": "OK"}> dB. test0.update ({"_ id": 15 },{$ push: {"test1": "CCC" }});> dB. test0.find ({"_ id": 15}); {"_ id": {"floatapprox": 15}, "Count": 18, "test1 ": ["AAA", "BBB", "CCC"], "test4": "testv4", "test5": "OK"}> dB. test0.update ({"_ id": 15 },{$ push: {"Test2": "CCC" }});> dB. test0.find ({"_ id": 15}); {"_ id": {"floatapprox": 15}, "Count": 18, "test1 ": ["AAA", "BBB", "CCC"], "Test2": ["CCC"], "test4": "testv4", "test5 ": "OK"}> dB. test0.update ({"_ id": 15}, {$ push: {"test1": ["DDD", "eee"]});> dB. test0.find ({"_ id": 15}); {"_ id": {"floatapprox": 15}, "Count": 18, "test1 ": ["AAA", "BBB", "CCC", ["DDD", "eee"], "Test2": ["CCC"], "test4 ": "testv4", "test5": "OK "}

5) $ pushall

Usage: {$ pushall: {field: value_array }}

The same as $ push, but multiple values can be appended to an array field at a time. Example:

> DB. test0.find ({"_ id": 15 });
{"_ Id": {"floatapprox": 15}, "Count": 18, "test1": ["AAA", "BBB", "CCC ", ["DDD", "eee"], "Test2": ["CCC"], "test4": "testv4", "test5": "OK "}

> DB. test0.update ({"_ id": 15 },{ $ pushall: {"test1": ["fff", "ggg"]});
> DB. test0.find ({"_ id": 15 });
{"_ Id": {"floatapprox": 15}, "Count": 18, "test1": ["AAA", "BBB", "CCC ", ["DDD", "eee"], "fff", "ggg"], "Test2": ["CCC"], "test4": "testv4 ", "test5": "OK "}

6)$ Addtoset

Usage: {$ addtoset: {field: Value} adds a value to the array. This value is added only when it is not in the array. Example:> dB. test0.update ({"_ id": 15 },{$ addtoset: {"test1" :{$ each: ["444", "555"] }});> DB. test0.find ({"_ id": 15}); {"_ id": {"floatapprox": 15}, "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" :{$ each: ["444", "555"] }});> DB. test0.find ({"_ id": 15}); {"_ id": {"floatapprox": 15}, "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) $ popDelete a value in an array usage: Delete the last value: {$ POP: {field: 1 }}
Delete the first value: {$ POP: {field:-1}. Note that only one value can be deleted, that is, only 1 or-1 can be used, you cannot use 2 or-2 to delete two objects. MongoDB 1.1 and later versions are available, 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) $ pullUsage: $ 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) $ pullallUsage: {$ pullall: {field: value_array} is the same as $ pull. Multiple values in the array can be deleted at a 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 a certain item in the array identified by conditions. Haha. 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 ': 'job'}, {$ Inc: {'comments. $. votes ': 1 }}, false, true)> T. find () {"_ id": objectid ("4b97e62bf1d8c7152c9ccb74"), "Title": "ABC", "Comments": [{"by": "Joe", "votes": 4 },{ "by": "Jane ", "votes": 7}]} note that $ will only apply the first array item found, and the following will not matter. Let's take a 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 (); note that when $ is used with $ unset, a null array item is left, but you can use {$ pull: {X: null} Delete All null array items. Example:> T. insert ({X: [,]})> T. Find () {"_ id": objectid ("4bde2ad3755d1_1_710e"), "X": [1, 2, 3, 4, 3, 2, 3, 4]}> T. update ({X: 3}, {$ unset: {"x. $ ": 1 }})> T. find () {"_ id": objectid ("4bde2ad3755d%%710e"), "X": [1, 2, null, 4, 3, 2, 3, 4]} {"_ id": objectid ("4b9e4a1fc583fa1c76198319"), "X": [1, 3, 3, 2]}

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.