I didn’t record the update operation before, so I have time to record the use of mongodb update today
Look at the syntax first:
db.collection.update( , , )
The syntax is very simple, but it will be more complicated to use with some functions. Let me explain.
query: conditional limitation, that is, the condition that the row needs to be updated, which is equivalent to the conditional judgment after where in SQL
update: equivalent to the set statement in the SQL table
options: What needs to be noted here is the update operation of mongodb. By default, only the first row that meets the conditions is updated. If you want to update all the rows that meet the conditions, you need to specify multi to be true, and one parameter is upsert, which means if it does not exist The records that match the query are inserted into the updated data. The default is false and not inserted. You can specify true
The simplest example, let’s take a look:
PRIMARY> db.test.save({id:1,”name”:”hank”})
PRIMARY> db.test.find()
{ “_id” : ObjectId(“50c055656b41f59a0235881b”), “id” : 1, “name” :
“hank” }
PRIMARY> db.test.update({id:1},{“name”:”dazuiba”})
PRIMARY> db.test.find()
{ “_id” : ObjectId(“50c055656b41f59a0235881b”), “name” : “dazuiba”
}
An example of updating a row by default:
PRIMARY> db.test.find()
{ “_id” : ObjectId(“50c056876b41f59a0235881c”), “id” : 1, “name” :
“hank” }
{ “_id” : ObjectId(“50c056886b41f59a0235881d”), “id” : 1, “name” :
“hank” }
{ “_id” : ObjectId(“50c056896b41f59a0235881e”), “id” : 1, “name” :
“hank” }
{ “_id” : ObjectId(“50c0568a6b41f59a0235881f”), “id” : 1, “name” :
“hank” }
PRIMARY> db.test.update({id:1},{“name”:”dazuiba”})
PRIMARY> db.test.find()
{ “_id” : ObjectId(“50c056876b41f59a0235881c”), “name” : “dazuiba”
}
{ “_id” : ObjectId(“50c056886b41f59a0235881d”), “id” : 1, “name” :
“hank” }
{ “_id” : ObjectId(“50c056896b41f59a0235881e”), “id” : 1, “name” :
“hank” }
{ “_id” : ObjectId(“50c0568a6b41f59a0235881f”), “id” : 1, “name” :
“hank” }
Update all matching rows:
PRIMARY> db.test.find()
{ “_id” : ObjectId(“50c056e46b41f59a02358820”), “id” : 1, “name” :
“hank” }
{ “_id” : ObjectId(“50c056e56b41f59a02358821”), “id” : 1, “name” :
“hank” }
{ “_id” : ObjectId(“50c056e66b41f59a02358822”), “id” : 1, “name” :
“hank” }
{ “_id” : ObjectId(“50c056e86b41f59a02358823”), “id” : 1, “name” :
“hank” }
PRIMARY>
db.test.update({id:1},{“name”:”dazuiba”},false,true)
–report an error, please pay attention here, $ related operations are required
multi update only works with $ operators
PRIMARY>
db.test.update({id:1},{$set:{“name”:”dazuiba”}},false,true)
PRIMARY> db.test.find()
{ “_id” : ObjectId(“50c056e46b41f59a02358820”), “id” : 1, “name” :
“dazuiba” }
{ “_id” : ObjectId(“50c056e56b41f59a02358821”), “id” : 1, “name” :
“dazuiba” }
{ “_id” : ObjectId(“50c056e66b41f59a02358822”), “id” : 1, “name” :
“dazuiba” }
{ “_id” : ObjectId(“50c056e86b41f59a02358823”), “id” : 1, “name” :
“dazuiba” }
Update rows that don’t exist, and insert them if they don’t:
PRIMARY> db.test.find()
{ “_id” : ObjectId(“50c058256b41f59a02358828”), “id” : 1, “name” :
“hank” }
{ “_id” : ObjectId(“50c058266b41f59a02358829”), “id” : 1, “name” :
“hank” }
{ “_id” : ObjectId(“50c058266b41f59a0235882a”), “id” : 1, “name” :
“hank” }
{ “_id” : ObjectId(“50c058276b41f59a0235882b”), “id” : 1, “name” :
“hank” }
PRIMARY>
db.test.update({id:2},{$set:{“name”:”dazuiba”}},true,false)
PRIMARY> db.test.find()
{ “_id” : ObjectId(“50c058256b41f59a02358828”), “id” : 1, “name” :
“hank” }
{ “_id” : ObjectId(“50c058266b41f59a02358829”), “id” : 1, “name” :
“hank” }
{ “_id” : ObjectId(“50c058266b41f59a0235882a”), “id” : 1, “name” :
“hank” }
{ “_id” : ObjectId(“50c058276b41f59a0235882b”), “id” : 1, “name” :
“hank” }
{ “_id” : ObjectId(“50c05840638c411993e87a5c”), “id” : 2, “name” :
“dazuiba” }
As long as you understand the meaning of the two parameters, you can use them flexibly. The following are a few examples
PRIMARY> db.test.find()
{ “_id” : ObjectId(“50c0589e6b41f59a0235882c”), “id” : 1, “name” :
“hank” }
{ “_id” : ObjectId(“50c058a06b41f59a0235882d”), “id” : 1, “name” :
“hank” }
{ “_id” : ObjectId(“50c058a16b41f59a0235882e”), “id” : 1, “name” :
“hank” }
{ “_id” : ObjectId(“50c058a16b41f59a0235882f”), “id” : 1, “name” :
“hank” }
PRIMARY>
db.test.update({id:1},{$set:{“name”:”dazuiba”}},true,true)
PRIMARY> db.test.find()
{ “_id” : ObjectId(“50c0589e6b41f59a0235882c”), “id” : 1, “name” :
“dazuiba” }
{ “_id” : ObjectId(“50c058a06b41f59a0235882d”), “id” : 1, “name” :
“dazuiba” }
{ “_id” : ObjectId(“50c058a16b41f59a0235882e”), “id” : 1, “name” :
“dazuiba” }
{ “_id” : ObjectId(“50c058a16b41f59a0235882f”), “id” : 1, “name” :
“dazuiba” }
PRIMARY>
db.test.update({id:2},{$set:{“name”:”hank”}},true,true)
PRIMARY> db.test.find()
{ “_id” : ObjectId(“50c0589e6b41f59a0235882c”), “id” : 1, “name” :
“dazuiba” }
{ “_id” : ObjectId(“50c058a06b41f59a0235882d”), “id” : 1, “name” :
“dazuiba” }
{ “_id” : ObjectId(“50c058a16b41f59a0235882e”), “id” : 1, “name” :
“dazuiba” }
{ “_id” : ObjectId(“50c058a16b41f59a0235882f”), “id” : 1, “name” :
“dazuiba” }
{ “_id” : ObjectId(“50c058ea638c411993e87a5d”), “id” : 2, “name” :
“hank” }
$Related operations will be introduced next time