Mongodb_ modifier ($inc/$set/$unset/$push/$pop/upsert ... )

Source: Internet
Author: User
Tags findone

Transferred from: http://blog.csdn.net/mcpang/article/details/7752736

For documents that are updated in addition to replacements, only partial updates to one or more documents are required to use the atom's update modifier to efficiently document updates. The update modifier is a special key in the
Used to specify complex operations, such as adding, deleting, or adjusting keys, and possibly manipulating arrays or inline documents.

1. $inc
--------------------------------------------------------------------------
What does this modifier do? Take a look at the results of the following example to see what happens.

Sample document: {"UID": "201203", "type": "1", size:10}

> Db.b.insert ({"UID": "201203", "type": "1", size:10})
> Db.b.find ()
{"_id": ObjectId ("5003b6135af21ff428dafbe6"), "UID": "201203", "type": "1",
"Size": 10}
> db.b.update ({"UID": "201203"},{"$inc": {"size": 1}})
> Db.b.find ()
{"_id": ObjectId ("5003b6135af21ff428dafbe6"), "UID": "201203", "type": "1",
"Size": 11}
> db.b.update ({"UID": "201203"},{"$inc": {"Size": 2}})
> Db.b.find ()
{"_id": ObjectId ("5003b6135af21ff428dafbe6"), "UID": "201203", "type": "1",
"Size": 13}
> db.b.update ({"UID": "201203"},{"$inc": {"Size":-1}})
> Db.b.find ()
{"_id": ObjectId ("5003b6135af21ff428dafbe6"), "UID": "201203", "type": "1",
"Size": 12}

It is concluded that the modifier $inc can be used to add or subtract a key to a value of a document that is a numeric type (only for the number that satisfies the requirement).
(Here's a question: In the previous article, when the update defaults to only the first document in a recordset that meets the criteria, is it the same after using the $inc modifier?). )

2. $set
-------------------------------------------------------------------
Used to specify a key and update the key value if the key does not exist and is created. Take a look at the following effects:

> Db.a.findone ({"UID": "20120002", "Type": "3"})
{"_id": ObjectId ("500216de81b954b6161a7d8f"), "desc": "Hello world2!", "num"
: +, "sname": "JK", "Type": "3", "UID": "20120002"}
Where the--size key does not exist
> db.a.update ({"UID": "20120002", "Type": "3"},{"$set": {"size": 10}})
> Db.a.findone ({"UID": "20120002", "Type": "3"})
{"_id": ObjectId ("500216de81b954b6161a7d8f"), "desc": "Hello world2!", "num"
: +, "size": Ten, "sname": "JK", "Type": "3", "UID": "20120002"}
Where the--sname key exists
> db.a.update ({"UID": "20120002", "Type": "3"},{"$set": {"sname": "SSK"}})
> Db.a.find ()
{"_id": ObjectId ("500216de81b954b6161a7d8f"), "desc": "Hello world2!", "num"
: +, "size": Ten, "sname": "SSK", "Type": "3", "UID": "20120002"}
{"_id": ObjectId ("50026affdeb4fa8d154f8572"), "desc": "Hello world1!", "num"
: "Sname": "JK", "type": "1", "UID": "20120002"}
--Can change the value type of the key
> db.a.update ({"UID": "20120002", "Type": "3"},{"$set": {"sname": ["Java", ". Net", "C + +]}})
> Db.a.findone ({"UID": "20120002", "Type": "3"})
{
"_id": ObjectId ("500216de81b954b6161a7d8f"),
"desc": "Hello world2!",
"num": 40,
"Size": 10,
"Sname": [
"Java",
". Net",
"C + +"
],
"Type": "3",
"UID": "20120002"
}

For an inline document, $set How to update the inline document, see the following example:
Example document: {"name": "Toyota", "type": "SUV", "size": {"height": ten, "width": 5, "Length": 15}}

> Db.c.findone ({"Name": "Toyota"})
{
"_id": ObjectId ("5003be465af21ff428dafbe7"),
"Name": "Toyota",
"Type": "SUV",
"Size": {
"Height": 10,
"width": 5,
"Length": 15
}
}
> db.c.update ({"Name": "Toyota"},{"$set": {"Size.Height": 8}})
> Db.c.findone ({"Name": "Toyota"})
{
"_id": ObjectId ("5003be465af21ff428dafbe7"),
"Name": "Toyota",
"Type": "SUV",
"Size": {
"Height": 8,
"width": 5,
"Length": 15
}
}
> db.c.update ({"Name": "Toyota"},{"$set": {"Size.width": 7}})
> Db.c.findone ({"Name": "Toyota"})
{
"_id": ObjectId ("5003be465af21ff428dafbe7"),
"Name": "Toyota",
"Type": "SUV",
"Size": {
"Height": 8,
"width": 7,
"Length": 15
}
}
Visible: For inline documents when using the $set update, use the "." The way you connect.


3. $unset
----------------------------------------------------------------
You can literally see its meaning, mostly to remove a key.
The sample operation works as follows:

> db.a.update ({"UID": "20120002", "Type": "3"},{"$unset": {"sname": 1}})
> Db.a.findone ({"UID": "20120002", "Type": "3"})
{
"_id": ObjectId ("500216de81b954b6161a7d8f"),
"desc": "Hello world2!",
"num": 40,
"Size": 10,
"Type": "3",
"UID": "20120002"
}
> db.a.update ({"UID": "20120002", "Type": "3"},{"$unset": {"num": 0}})
> Db.a.findone ({"UID": "20120002", "Type": "3"})
{
"_id": ObjectId ("500216de81b954b6161a7d8f"),
"desc": "Hello world2!",
"Size": 10,
"Type": "3",
"UID": "20120002"
}
> db.a.update ({"UID": "20120002", "Type": "3"},{"$unset": {"Size":-1}})
> Db.a.findone ({"UID": "20120002", "Type": "3"})
{
"_id": ObjectId ("500216de81b954b6161a7d8f"),
"desc": "Hello world2!",
"Type": "3",
"UID": "20120002"
}
> db.a.update ({"UID": "20120002", "Type": "3"},{"$unset": {"desc": "Sssssss"}})
> Db.a.findone ({"UID": "20120002", "Type": "3"})
{
"_id": ObjectId ("500216de81b954b6161a7d8f"),
"Type": "3",
"UID": "20120002"
}

Conclusion: When using modifier $unset, the target key can be deleted regardless of the target key using 1, 0,-1, or a specific string.

4. Array modifier--$push
------------------------------------------------------------------
The sample operation works as follows:
> Db.c.find ()
{"_id": ObjectId ("5003be465af21ff428dafbe7"), "name": "Toyota", "type": "SUV",
"Size": {"height": 8, "width": 7, "Length": 15}}

--first push a key that does not exist in the current document title
> db.c.update ({"Name": "Toyota"},{$push: {"title": "T1"}})
> Db.c.find ()
{"_id": ObjectId ("5003be465af21ff428dafbe7"), "name": "Toyota", "size": {"height": 8,
"width": 7, "Length": "["], "title": ["T1"], "type": "SUV"}

--Push a value to the title again
> db.c.update ({"Name": "Toyota"},{$push: {"title": "T2"}})
> Db.c.find ()
{"_id": ObjectId ("5003be465af21ff428dafbe7"), "name": "Toyota", "size": {"height": 8,
"width": 7, "Length": "["], "title": ["T1", "T2"], "type": "SUV"}

--Push a value to the title again
> db.c.update ({"Name": "Toyota"},{$push: {"title": "T2"}})
> Db.c.find ()
{"_id": ObjectId ("5003be465af21ff428dafbe7"), "name": "Toyota", "size": {"height": 8,
"width": 7, "Length": "["], "title": ["T1", "T2", "T2"], "type": "SUV"}

--Push a value to a key that already exists for a non-array type of key value
> db.c.update ({"Name": "Toyota"},{$push: {"Size.Height": 10}})
Cannot apply $push/$pushAll modifier to Non-array
> db.c.update ({"Name": "Toyota"},{$push: {"name": "Ddddddd"}})
Cannot apply $push/$pushAll modifier to Non-array

To conclude: $push--Adds an array element to the key of an array type in the document, and does not filter duplicate data. When you add a key that exists, requires that the key value type must be an array, and the key does not exist, the key of the array type is created.

5. Array modifier--$ne/$addToSet
---------------------------------------------------------------------
When adding an element primarily to an array type key value, avoid generating duplicate data in the array, $ne in some cases is not in transit.

> db.c.update ({"title": {$ne: "T2"}},{$push: {"title": "T2"}})
> Db.c.find ()
{"_id": ObjectId ("5003be465af21ff428dafbe7"), "name": "Toyota", "size": {"height": 8,
"width": 7, "Length": "["], "title": ["T1", "T2", "T2"], "type": "SUV"}

> db.c.update ({"Name": "Toyota"},{$addToSet: {"title": "T2"}})
> Db.c.find ()
{"_id": ObjectId ("5003be465af21ff428dafbe7"), "name": "Toyota", "size": {"height": 8,
"width": 7, "Length": "["], "title": ["T1", "T2", "T2"], "type": "SUV"}

6. Array modifier--$pop, $pull
------------------------------------------------------------
$pop remove elements from an array from the head or tail of an array, as an example:
{"_id": ObjectId ("5003be465af21ff428dafbe7"), "name": "Toyota", "size": {"height": 8,
"width": 7, "Length": "["], "title": ["T1", "T2", "T3", "T4"], "type": "SUV"}

--Remove 1 from the tail of the array
> db.c.update ({"Name": "Toyota"},{$pop: {"title": 1}})
> Db.c.find ()
{"_id": ObjectId ("5003be465af21ff428dafbe7"), "name": "Toyota", "size": {"height": 8,
"width": 7, "Length": "["], "title": ["T1", "T2", "T3"], "type": "SUV"}
--from the head of the array-1
> db.c.update ({"Name": "Toyota"},{$pop: {"title":-1}})
> Db.c.find ()
{"_id": ObjectId ("5003be465af21ff428dafbe7"), "name": "Toyota", "size": {"height": 8,
"width": 7, "Length": "["], "title": ["T2", "T3"], "type": "SUV"}
--Remove 0 from the tail of the array
> db.c.update ({"Name": "Toyota"},{$pop: {"title": 0}})
> Db.c.find ()
{"_id": ObjectId ("5003be465af21ff428dafbe7"), "name": "Toyota", "size": {"height": 8,
"width": 7, "Length": "["], "title": ["T2"], "type": "SUV"}

$pull remove the element that satisfies the condition from the array, the example is as follows:
{"_id": ObjectId ("5003be465af21ff428dafbe7"), "name": "Toyota", "size": {"height": 8,
"width": 7, "Length": "["], "title": ["T1", "T2", "T2", "T3"], "type": "SUV"}

> db.c.update ({"Name": "Toyota"},{$pull: {"title": "T2"}})
> Db.c.find ()
{"_id": ObjectId ("5003be465af21ff428dafbe7"), "name": "Toyota", "size": {"height": 8,
"width": 7, "Length": "["], "title": ["T1", "T3"], "type": "SUV"}

7. Positioning modifier for Arrays
-------------------------------------------------------------------
The position or position operator ("$") can be used when the value in the array needs to be manipulated. Arrays are starting at 0, and you can select elements directly as keys.
Examples are as follows:
{"UID": "001", comments:[{"name": "T1", "Size": 10},{"name": "T2", "Size": 12}]}

> Db.c.find ({"UID": "001"})
{"_id": ObjectId ("5003da405af21ff428dafbe8"), "UID": "001", "comments": [{
"Name": "T1", "Size": ten}, {"name": "T2", "Size": 12}]}
> db.c.update ({"UID": "001"},{$inc: {"comments.0.size": 1}})
> Db.c.find ({"UID": "001"})
{"_id": ObjectId ("5003da405af21ff428dafbe8"), "UID": "001", "comments": [{
"Name": "T1", "Size": one}, {"name": "T2", "Size": 12}]}
> db.c.update ({"Comments.name": "T1"},{$set: {"comments.$.size": 1}})
> Db.c.find ({"UID": "001"})
{"_id": ObjectId ("5003da405af21ff428dafbe8"), "UID": "001", "comments": [{
"Name": "T1", "Size": 1}, {"name": "T2", "Size": 12}]}

--If the criteria are met for multiple documents, only the first document is updated.

8.upsert
-----------------------------------------------------------------
Upsert is a special kind of update. When there are no qualified documents, create a new document based on this condition and update the document, and if a matching document is found, the update will be normal.
Using Upsert, you can either avoid race problems or reduce the amount of code (the third parameter of update represents this Upsert, when the argument is true)

> Db.c.remove ()
> db.c.update ({"Size": 11},{$inc: {"Size": 3}})
> Db.c.find ()
> db.c.update ({"Size": 11},{$inc: {"size": 3}},false)
> Db.c.find ()
> db.c.update ({"Size": 11},{$inc: {"size": 3}},true)
> Db.c.find ()
{"_id": ObjectId ("5003ded6c28f67507a6df1de"), "size": 14}

9.save function
-----------------------------------------------------------------
1. Can be inserted when the document does not exist, updated when it exists, only one parameter document.
2. If the document contains "_id", the Upsert will be called. Otherwise, the insert is called.
> Db.a.find ()
{"_id": ObjectId ("50026affdeb4fa8d154f8572"), "desc": "Hello world1!", "num": 50,
"Sname": "JK", "type": "1", "UID": "20120002"}
> var o = db.a.findone ()
> O.num = 55
55
> Db.a.save (o)
> Db.a.find ()
{"_id": ObjectId ("50026affdeb4fa8d154f8572"), "desc": "Hello world1!", "num": 55,
"Sname": "JK", "type": "1", "UID": "20120002"}

Mongodb_ modifier ($inc/$set/$unset/$push/$pop/upsert ... )

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.