MongoDB Update Document

Keywords: MongoDB SQL shell REST

The commands in this article are all real-time under the mongo shell. In real web projects, the corresponding implementations of these commands are encapsulated, so there is no need to be scared.

Put on the official orthodox documents first English document The links below are all from the Chinese community.

There are many ways for MongoDB to update documents, especially after 3.2. There are several main ways for MongoDB to update documents.

db.collection.updateOne() Even though there may be multiple documents matched by filtering conditions, only one document is updated at most. 3.2 New Version Function.

db.collection.updateMany() Update all documents matched by filtering conditions. 3.2 new version function.

db.collection.replaceOne() Even though there may be multiple documents matched by filtering conditions, only one document is replaced at most. 3.2 New Version Function.

db.collection.update() Even though there may be multiple documents matched by filtering conditions, only one document is updated or replaced at most. By default, only one document is updated. To update multiple documents, use multi option

db.collection.save() Replace existing documents with incoming documents.

This article only explains the update() method, and the rest of the posts have built-in links to see official documents.

update() grammar

db.collection.update(
   <filter>,
   <update>,
   {
     upsert: <boolean>,
     writeConcern: <document>,
     collation: <document>
   }
)

Description of parameters:

  • Query: the query condition of update, similar to where condition in sql update query.
  • Update: The object of update and some updated operators (such as $set,$inc...) can also be understood as the following set in the sql update query
  • upsert: Optional. This parameter means whether to insert a new document if there is no update record. true is insert, default is false, no insert.
  • multi: Optionally, mongodb defaults to false, updates only the first record found, and if this parameter is true, updates all the records found on condition.
  • Write Concern: Optional, throw exception level.

update filter document grammar

1. Equal query

{ <field1>: <value1>, <field2>: <value2>...}

Expressions specify equality conditions, queries satisfy all documents < field > equal to < value >, and multiple sets of conditions can be specified.

2. Query operators to specify query conditions

{ <field1>: { <operator1>: <value1> }, ... }

The specified operator query conditions need to be nested {}, such as query age fields larger than 20

{"age" : {$gt: 20}}

Let's move on to the last article to test, starting with a look at the records of users documents

> db.users.find().pretty()
{
    "_id" : ObjectId("5c77461437955b945af7321f"),
    "name" : "The edge is you.",
    "age" : 27,
    "gender" : "male"
}
{
    "_id" : ObjectId("5c77499137955b945af73220"),
    "name" : "tom",
    "age" : 22,
    "gender" : "male"
}
{
    "_id" : ObjectId("5c77499137955b945af73221"),
    "name" : "Linda",
    "age" : 18,
    "gender" : "female"
}
{
    "_id" : ObjectId("5c77731d37955b945af73222"),
    "name" : "Hanmeimei",
    "age" : 17,
    "gender" : "female"
}
{
    "_id" : ObjectId("5c7774b736397a1c0aa425ea"),
    "name" : "Jack",
    "age" : 42,
    "gender" : "male"
}
{
    "_id" : ObjectId("5c7774b736397a1c0aa425eb"),
    "name" : "Lucy",
    "age" : 23,
    "gender" : "female"
}

pretty() method can beautify the output at the terminal

We changed tom's age to 20

> db.users.update({"name" : "tom"}, {$set : {"age" : 20}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.find()
{ "_id" : ObjectId("5c77461437955b945af7321f"), "name" : "The edge is you.", "age" : 27, "gender" : "male" }
{ "_id" : ObjectId("5c77499137955b945af73220"), "name" : "tom", "age" : 20, "gender" : "male" }
{ "_id" : ObjectId("5c77499137955b945af73221"), "name" : "Linda", "age" : 18, "gender" : "female" }
{ "_id" : ObjectId("5c77731d37955b945af73222"), "name" : "Hanmeimei", "age" : 17, "gender" : "female" }
{ "_id" : ObjectId("5c7774b736397a1c0aa425ea"), "name" : "Jack", "age" : 42, "gender" : "male" }
{ "_id" : ObjectId("5c7774b736397a1c0aa425eb"), "name" : "Lucy", "age" : 23, "gender" : "female" }

There's no need to beautify it by pretty() method. It's too long ____________

Looking at the second document, tom's age was changed to 20

What should we do to change Jack and Lucy's age to 25?

> db.users.update({"name" : {$in : ["Jack", "Lucy"]}}, {$set: {"age" : 25}})
WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
> db.users.find()
{ "_id" : ObjectId("5c77461437955b945af7321f"), "name" : "The edge is you.", "age" : 27, "gender" : "male" }
{ "_id" : ObjectId("5c77499137955b945af73220"), "name" : "tom", "age" : 20, "gender" : "male" }
{ "_id" : ObjectId("5c77499137955b945af73221"), "name" : "Linda", "age" : 18, "gender" : "female" }
{ "_id" : ObjectId("5c77731d37955b945af73222"), "name" : "Hanmeimei", "age" : 17, "gender" : "female" }
{ "_id" : ObjectId("5c7774b736397a1c0aa425ea"), "name" : "Jack", "age" : 25, "gender" : "male" }
{ "_id" : ObjectId("5c7774b736397a1c0aa425eb"), "name" : "Lucy", "age" : 23, "gender" : "female" }

Question: Only Jack's age has been updated to 25 and Lucy's has not. As mentioned earlier, update() only updates the first record matched. To update more than one record, you need to set multi parameters. Look at the next command

> db.users.update({"name" : {$in : ["Jack", "Lucy"]}}, {$set: {"age" : 28}}, {multi : true})
WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
> db.users.find()
{ "_id" : ObjectId("5c77461437955b945af7321f"), "name" : "The edge is you.", "age" : 27, "gender" : "male" }
{ "_id" : ObjectId("5c77499137955b945af73220"), "name" : "tom", "age" : 20, "gender" : "male" }
{ "_id" : ObjectId("5c77499137955b945af73221"), "name" : "Linda", "age" : 18, "gender" : "female" }
{ "_id" : ObjectId("5c77731d37955b945af73222"), "name" : "Hanmeimei", "age" : 17, "gender" : "female" }
{ "_id" : ObjectId("5c7774b736397a1c0aa425ea"), "name" : "Jack", "age" : 28, "gender" : "male" }
{ "_id" : ObjectId("5c7774b736397a1c0aa425eb"), "name" : "Lucy", "age" : 28, "gender" : "female" }

The {multi: true} parameter is added, so the age s of Jack and Lucy are set to 28. What if the updated document does not exist? Let's try to set upsert to true and see the next command

> db.users.update({"name":"Lucys"}, {$set : {"is_active": 0}}, {upsert: true})
WriteResult({
    "nMatched" : 0,
    "nUpserted" : 1,
    "nModified" : 0,
    "_id" : ObjectId("5c77946b96c95d22dfe8b411")
})
> db.users.find()
{ "_id" : ObjectId("5c77461437955b945af7321f"), "name" : "The edge is you.", "age" : 27, "gender" : "male" }
{ "_id" : ObjectId("5c77499137955b945af73220"), "name" : "tom", "age" : 20, "gender" : "male" }
{ "_id" : ObjectId("5c77499137955b945af73221"), "name" : "Linda", "age" : 18, "gender" : "female" }
{ "_id" : ObjectId("5c77731d37955b945af73222"), "name" : "Hanmeimei", "age" : 17, "gender" : "female" }
{ "_id" : ObjectId("5c7774b736397a1c0aa425ea"), "name" : "Jack", "age" : 28, "gender" : "male" }
{ "_id" : ObjectId("5c7774b736397a1c0aa425eb"), "name" : "Lucy", "age" : 28, "gender" : "female" }
{ "_id" : ObjectId("5c77946b96c95d22dfe8b411"), "name" : "Lucys", "is_active" : 0 }

You can see that there is a new document in mongodb. Besides the _id field, the other fields appear in the update statement, but not in the document. You can also set upsert and multi parameters in this way.

> db.users.update({"age": {$gt: 25}}, {$set: {"is_active": 1}}, false, true)
WriteResult({ "nMatched" : 3, "nUpserted" : 0, "nModified" : 3 })
> db.users.find()
{ "_id" : ObjectId("5c77461437955b945af7321f"), "name" : "The edge is you.", "age" : 27, "gender" : "male", "is_active" : 1 }
{ "_id" : ObjectId("5c77499137955b945af73220"), "name" : "tom", "age" : 20, "gender" : "male" }
{ "_id" : ObjectId("5c77499137955b945af73221"), "name" : "Linda", "age" : 18, "gender" : "female" }
{ "_id" : ObjectId("5c77731d37955b945af73222"), "name" : "Hanmeimei", "age" : 17, "gender" : "female" }
{ "_id" : ObjectId("5c7774b736397a1c0aa425ea"), "name" : "Jack", "age" : 28, "gender" : "male", "is_active" : 1 }
{ "_id" : ObjectId("5c7774b736397a1c0aa425eb"), "name" : "Lucy", "age" : 28, "gender" : "female", "is_active" : 1 }
{ "_id" : ObjectId("5c77946b96c95d22dfe8b411"), "name" : "Lucys", "is_active" : 0 }

The first parameter false is the value of upsert, and the second true is the value of multi.

Posted by blawson7 on Thu, 09 May 2019 01:27:39 -0700