MongoDB-----Only partially update the update modifier that can use atoms for one or more documents

Source: Internet
Author: User
Tags findone modifier modifiers numeric value

Update ()

db.. Update ( <query> <update>, {< Span class= "PLN" > Upsert: <boolean> ,  Multi: <boolean> , Writeconcern: < Document> })     

Db.collection.update (criteria, objnew, Upsert, multi) Four parameters are described below:

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.

1. $inc

Modifier $inc You can add or subtract a key to a document that has a numeric value that can only be used to satisfy the required number. Digital Plus and minus operations

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}

2. $set

Used to specify a key and update the key value, if the key does not exist and creates a new piece of data

Db.a.findone ({"UID": "20120002", "Type": "3"})

{"_id": ObjectId ("500216de81b954b6161a7d8f"), "desc": "Hello world2!", "num": +, "sname": "JK", "Type": "3", "UID": "20120002"}

1) 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"}

2)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"}

3) The value type of the key can be changed
> 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"
}

4) For embedded documents, $set How to update the embedded 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

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

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"
}

4, array modifier--$push

$push-Adds an array element to the key of an array type in the document, without filtering the duplicated 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.

> 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"}
 
--then push a value in title
> 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

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"}

Array modifiers-$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 an element that satisfies a condition from an array

{"_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"}

Positioning modifiers 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.

{"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.

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}

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-----Only partially update the update modifier that can use atoms for one or more documents

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.