1024programmer Mongodb Explanation of update operation of MongoDB database

Explanation of update operation of MongoDB database

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

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/explanation-of-update-operation-of-mongodb-database/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索