In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Modify a simple document:
Db.getCollection ("test") .insert ({title: "Shopping list 1", amount: 35, detail: [{name: "Apple", price: 22}, {name: "flour", price: 18}]}) / / modify the eligible entry (inserted, the earliest) db.getCollection ("test") .update ({title: "Commodity Shopping list 1"}, {$set: {title: "Commodity Shopping list 2"}})
(update, modify the one that meets the criteria (inserted, the earliest one that meets the criteria)
Update fields (increase, decrease):
Db.getCollection ("test") .update ({title: "merchandise list 1"}, {$inc: {"amount": 5}})
(update, modify the one that meets the criteria (inserted, the earliest one that meets the criteria). The value after the $inc operator, which can be positive, negative, or decimal)
Specify multiples instead:
Db.getCollection ("test") .update ({title: "merchandise list 1"}, {$mul: {"amount": 2}})
(update, modify the one that meets the criteria (inserted, the earliest one that meets the criteria). The value after the $mul operator, which can be positive, negative, or decimal)
Update field / key name
Db.getCollection ("test") .insert ({_ id: 66, titlss: 35, amount: 50.5, detail: [{name: "Apple", price: 22}, {name: "flour", price: 18}]}) Db.getCollection ("test") .update ({_ id: 66}, {$rename: {"titlss": "title"}})
($rename operator. Especially useful in cases where there are a large number of key name errors)
Remove the column whose _ id is 66 and the title is 35 (the title of other data bars remains the same)
Db.getCollection ("test") .update ({_ id: 66}, {$unset: {"title": "35"}})
(the title of other data bars remains unchanged, and the one with a title of 35 becomes NCMA)
The amount of the _ id:66 document is changed from 50.5 to 50:
Db.getCollection ("test") .update ({_ id: 66}, {$unset: {"title": "35"}})
(compare the value given by $min with the current document field value, and modify the current document value to the given value when the given value is small)
The amount of the _ id:66 document is changed from 50 to 50.5:
Db.getCollection ("test") .update ({_ id: 66}, {$max: {"amount": 50.5}})
(compare the value given by $max with the current document field value, and modify the current document value to the given value when the given value is large)
ISODate time method:
Db.getCollection ("test") .insert ({_ id: 69, title: "Shopping list 6", amount: 80, unit: "Yuan", detail: [{name: "Apple", price: 22}, {name: "flour", price: 18}] Lasttime: ISODate ("2018-07-21 14:53:56")})
Modify arrays and nested documents in a document:
Db.getCollection ("test") .insert ({_ id: 80, title: "Shopping list 80", amount: 80, unit: "Yuan", detail: [{name: "Apple", price: 22}, {name: "flour", price: 18}] Lasttime: ISODate ("2018-07-21 14:53:56"), overview: {shop: "Tintin E-commerce platform", shopno: 5, address: "Global Village"}) Db.getCollection ("test") .update ({_ id: 80}, {$set: {"detail.1": {name: "rice", price: 16}, "overview.address": "some sea"}})
(the subscript of the MongoDB array starts at 0, and "detail.1" represents the second element of the array)
(when referencing an array or embedding document objects, you need to add a "" sign, such as "detail.1", "overview.address", separated by a period.)
Modify the contents of the array, with
By default, the update command performs an action to modify a document, and we also want to modify all eligible document records at the same time. Here we need to use the multi option:
Db.getCollection ("test") .update ({"detail.name": "flour", "detail.price": {$lte: 30}}, {$set: {"detail.1": {name: "flour", price: 666}, "overview.address": "here"}, {multi: true})
(change the flour price of the second (subscript 1) of the recorded detail array whose flour price is less than or equal to 30 to 40)
Find () query condition operator, which can be used on update operation conditions
Db.getCollection ("test") .update ({"detail.name": "flour", "detail.price": {$lte: 30}}, {$set: {"detail.1": {name: "flour", price:40}, {multi: true / / modify} if the conditions meet)
The Update command can increase the parity segment of the document under certain circumstances, and even realize the function of insert command. This specific condition is that the document to be modified has no fields to modify, and the update command has the upsert option
Db.getCollection ("test") .update ({_ id: 80}, {$set: {"detail.1": {name: "flour", price:10}, / / modified danwei: "USD" / / newly added fields}}, {upsert: true})
The writeConcern option provides an error handling mechanism for update when modifying data exceptions.
Db.getCollection ("test") .update ({item: "}, {$set: {title:" Test ", price: 50}}, {multi: true, writeconcern: {w:" majority ", wtimeout:3000}})
(when the update command is not completed within 5 seconds, cancel the command operation and return an error value)
Db.collection.updateOne (). The only difference from update () is the absence of a multi: option in the command syntax, that is, updateOne () only applies to the modification task of a document that meets the criteria.
Db.collection.updateMany (). The only difference from update () is that a multi is missing from the command syntax:
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.