Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

MongoDB Note 3-Update Operation

2025-02-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

Update operation and query operation are extensive and profound. It needs to be understood. 1. Tough document replacement update operation 2. Error will be reported when the primary key conflicts and the update operation will be stopped because it is a tough replacement. When the replaced document conflicts with the existing document ID, the system will report an error.

3. InsertOrUpdate operation purpose: if the querier finds out the data, it will perform the update operation, and if it cannot find out, it will replace the operation.

Practice: Db.[ documentName] .update ({querier}, {modifier}, true)

4. Batch update operation by default when the querier queries multiple pieces of data, how to modify the first piece of data by default?

5. Use modifiers to complete partial update operation name syntax case Note $set {$set: {field:value}} {$set: {name: "uspcat"}} it is used to specify a key-value pair, and if a key exists, it is modified and added if it does not exist.

$inc {$inc: {field:value}} {$inc: {"count": 1}} only uses the numeric type, which can add and subtract the value of the numeric type corresponding to the specified key.

$unset {$unset: {filed:1}} {$unset: {"name": 1}

Its simple use is to delete the specified key.

$push {$push: {field: value}}

{$push: {books: "JS"}

1. If the specified key is an array append with a new value of 2. Interrupts the current operation Cannot apply $push/$pushAll modifier to non-array3 if the specified key is not an array. If the specified key does not exist, create a key-value pair of the array type $pushAll

{$pushAll: {field: array}} {$push: {books: ["EXTJS", "JS"]}

The usage is similar to $push. He can understand adding array data.

$addToSet

{$addToSet: {field: value}} {$addToSet: {books: "JS"}

If this item exists in the target array, it will not operate; if it does not exist, it will be added.

$pop

{$pop: {field: value}}

{$pop: {name:1}} {$pop: {name:-1}} Delete a value from the specified array 1 remove the last numeric value,-1 delete the first numeric value

$pull {$pull: {filed:value}} {$pull: {"book": "JS"}} Delete a specified value $pullAll {$pullAll: {filed:array}} {$pullAll: {"name": ["JS", "JAVA"]} delete multiple specified values at once ${$push: {field: value}}

{$push: {books: "JS"}

1. Array locator, if the array has multiple values and we only want to operate on some of them, we need to use the locator ($) example: for example, there are documents {name: "YFC", age:27,books: [{type:'JS',name: "EXTJS4"}, {type: "JS", name: "JQUERY"}, {type: "DB" Name: "MONGODB"}]} We want to add the same author to the document where type equals JS. Author is USPCAT method: db.text.update ({"books.type": "JS"}, {$set: {"books.$.author": "USPCAT"}}).

6. The combination of $addToSet and $each to complete batch numerical updates 8, the runCommand function and the findAndModify function runCommand can execute the special function findAndModify in mongoDB is one of the special functions he uses to return the document runCommand ({"findAndModify": "processes", query: {querier}, sort {sort}, new:true update: {updater}, remove:true}). Value ps = db.runCommand ({"findAndModify": "persons") "query": {"name": "text"}, "update": {"$set": {"email": "1221"}}, "new": true}) .valuedo_something (ps) http://www.cppblog.com/byc/archive/2011/07/15/151063.aspx

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report