mongodb modifier ($inc and $set and $unset and $push and $and upsert

  • 2020-06-15 10:25:36
  • OfStack

In addition to replacement, only partial update is needed for one or more documents. Atomic update modifier can be used to efficiently update documents. Update modifier is a special key in,
Used to specify complex operations, such as adding, deleting, or adjusting keys, and possibly manipulating arrays or embedded documents.

1.$inc

What does this modifier do? Take a look at the results in the following example.
Sample documents: {"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 }

The conclusion is that the modifier $inc can increase or decrease the key of a certain numeric value in the document (only the number that satisfies the requirement).
(Here's the question: The update defaults to only the first document in the recordset that meets the criteria mentioned in the previous article. Is it still the same after using the $inc modifier?)

2.$set

Used to specify 1 key and update the key value if the key does not exist and is created. Here's what it looks like:


> db.a.findOne({"uid" : "20120002","type" : "3"})
{ "_id" : ObjectId("500216de81b954b6161a7d8f"), "desc" : "hello world2!", "num"
: 40, "sname" : "jk", "type" : "3", "uid" : "20120002" }
--size Where a 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"
: 40, "size" : 10, "sname" : "jk", "type" : "3", "uid" : "20120002" }
--sname Where a key exists 
> db.a.update({"uid" : "20120002","type" : "3"},{"$set":{"sname":"ssk"}})
> db.a.find()
{ "_id" : ObjectId("500216de81b954b6161a7d8f"), "desc" : "hello world2!", "num"
: 40, "size" : 10, "sname" : "ssk", "type" : "3", "uid" : "20120002" }
{ "_id" : ObjectId("50026affdeb4fa8d154f8572"), "desc" : "hello world1!", "num"
: 50, "sname" : "jk", "type" : "1", "uid" : "20120002" }
-- 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"
}

How does $set update embedded documents for embedded documents? See the following example:
Sample document: {" name ":" toyota ", "type" : "suv", "size" : {" height ": 10," 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, use the "." join mode when updating with $set.

3.$unset

This can be seen literally and is mainly used to delete keys.
The results of the sample operation are 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: With the $unset modifier, you can delete the target key using either 1, 0, -1, or a concrete string, etc.

4. Array modifier --$push

The results of the sample operation are as follows:


> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "type" : "suv",
"size" : { "height" : 8, "width" : 7, "length" : 15 } }
-- First, push1 Three keys that do 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" : 15 }, "title" : [ "t1" ], "type" : "suv" }
 
-- Again to title In the push1 A value 
> db.c.update({"name" : "toyota"},{$push:{"title":"t2"}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
 "width" : 7, "length" : 15 }, "title" : [ "t1", "t2" ], "type" : "suv" }
-- Again to title In the push1 A value 
> db.c.update({"name" : "toyota"},{$push:{"title":"t2"}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
 "width" : 7, "length" : 15 }, "title" : [ "t1", "t2", "t2" ], "type" : "suv" }
-- Again to 1 Six keys of non-array type that already exist push1 A 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

Conclusion: $push- adds 1 array element to an array type key in the document without filtering duplicate data. When the key is added, the key value type must be an array. A key of type array is created when the key does not exist.

Array modifier --$ne/$addToSet

When adding 1 element to the key value of the array type, avoid duplication of data in the array. $ne is not in line in some cases.


> db.c.update({"title" : {$ne:"t2"}},{$push:{"title":"t2"}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
 "width" : 7, "length" : 15 }, "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" : 15 }, "title" : [ "t1", "t2", "t2" ], "type" : "suv" }

6. Array modifier --$pop, $pull

$pop removes an element from the head or tail of an array, as shown below:


{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
 "width" : 7, "length" : 15 }, "title" : [ "t1", "t2", "t3", "t4" ],"type" : "suv" }
-- Delete from the end 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" : 15 }, "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" : 15 }, "title" : [ "t2", "t3" ], "type" : "suv" }
-- Delete from the end of the array  0
> db.c.update({"name" : "toyota"},{$pop:{"title":0}})
> db.c.find()
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
 "width" : 7, "length" : 15 }, "title" : [ "t2" ], "type" : "suv" }
 
$pull Remove elements that meet the criteria from the array as follows: 
{ "_id" : ObjectId("5003be465af21ff428dafbe7"), "name" : "toyota", "size" : { "height" : 8,
 "width" : 7, "length" : 15 }, "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" : 15 }, "title" : [ "t1", "t3" ], "type" : "suv" }

7. Array location modifier

When you need to manipulate the value in the array, you can use the position or positioning operator ("$"). The array starts with 0, and you can select the element directly by using the subscript as the key.
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" : 10 }, { "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" : 11 }, { "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 more than one document meets the criteria, only the first is updated 1 A document. 

8.upsert

upsert is a special update. When there is no document that meets the criteria, a new document is created based on the criteria and the updated document. If a matching document is found, the update is normal.
With upsert, you can avoid race problems and reduce the amount of code (the third parameter of update represents this upsert, when the parameter 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. It can be inserted when the document does not exist and updated when it does exist, with only one parameter document.
2. If the document contains "_id", upsert is called. Otherwise, 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" }


Related articles: