MongoDB Update operation

Source: Internet
Author: User
Tags mongodb update

In addition to the query criteria, you can use the modifier to update the document. 1. $inc > Db.tianyc03.find () {"_id": ObjectId ("50ea6b6f12729d90ce6e341b"), "name": "Xtt", "Age":}> db.tianyc03 . Update ({name: ' Xtt ', age:11},{' $inc ': {age:5}}) > Db.tianyc03.find () {"_id": ObjectId ("50ea6b6f12729d90ce6e341b") , "name": "Xtt", "Age":}# $inc You can also add columns. > Db.tianyc04.find () {"_id": ObjectId ("510c83ad13d7e50c2281b048"), "WC": 1}> db.tianyc04.update ({wc:1},{$inc: { SCORE:50}) > Db.tianyc04.find () {"_id": ObjectId ("510c83ad13d7e50c2281b048"), "score": +, "WC": 1}>2. $set is used to specify a value for a key that is created if the key does not exist. If you need to remove the key, use $unset. #新增加列sex > Db.tianyc03.update ({name: ' Xtt ', age:16},{$set: {sex: ' m '}}) > Db.tianyc03.find () {"_id": ObjectId (" 50ea6b6f12729d90ce6e341b ")," age ": +," name ":" Xtt "," Sex ":" M "} #修改列sex > Db.tianyc03.update ({name: ' Xtt ', age:16},{ $set: {sex: ' Male '}) > Db.tianyc03.find () {"_id": ObjectId ("50ea6b6f12729d90ce6e341b"), "age": +, "name": "Xtt", "se X ":" Male "} #改变列sex数据类型 > Db.tianyc03.update ({name: ' Xtt ', age:16},{$Set:{sex:1}) > Db.tianyc03.find () {"_id": ObjectId ("50ea6b6f12729d90ce6e341b"), "age": +, "name": "Xtt", "Sex": 1 } #删除列sex > Db.tianyc03.update ({name: ' Xtt ', age:16},{$unset: {sex:1}}) > Db.tianyc03.find () {"_id": ObjectId (" 50ea6b6f12729d90ce6e341b ")," age ": +," name ":" Xtt "} 3. $push used to increase the array. If you do not change the column, it will automatically increase. > Db.tianyc03.find () {"_id": ObjectId ("50ea6b6f12729d90ce6e341b"), "age": +, "name": "Xtt"}> db.tianyc03.update ({' name ': ' Xtt '},{$push: {companies: ' neu '} ') > Db.tianyc03.find () {"_id": ObjectId ("50ea6b6f12729d90ce6e341b"), " Age ": +," companies ": [" Neu "]," name ":" Xtt "}> db.tianyc03.update ({' name ': ' Xtt '},{$push: {companies: ' Alibaba '}}) & Gt  Db.tianyc03.find () {"_id": ObjectId ("50ea6b6f12729d90ce6e341b"), "age": +, "companies": ["Neu", "Alibaba"], "name": "Xtt"}4. $addToSet The #push operation does not check if the elements in the array are duplicates, you need to use $addtoset> db.tianyc03.update ({' name ': ' Xtt '},{$addToSet If you need to add the non-repeating data to the array: { Companies: ' Alibaba '}) > Db.tianyc03.find () {"_id": ObjectId ("50ea6b6f12729d90ce6e341b ")," age ": +," companies ": [" Neu "," Alibaba "," Alibaba "]," name ":" Xtt "}> db.tianyc03.update ({' Name ': ' Xtt '},{$addToSet: {companies: ' Congxing '}}) > Db.tianyc03.find () {"_id": ObjectId ("50ea6b6f12729d90ce6e341b"), "Age": +, "companies": ["Neu", "Alibaba", "Alibaba", "congxing"], "name": "Xtt"}> db.tianyc03.update ({' name ': ' XT T '},{$addToSet: {companies: ' Congxing '}}) > Db.tianyc03.find () {"_id": ObjectId ("50ea6b6f12729d90ce6e341b"), "age" : +, "companies": ["Neu", "Alibaba", "Alibaba", "congxing"], "name": "Xtt"}>5. $pop to remove data from an array by $pop. At this point the data is considered a queue. Using {$pop: {key:1}} will remove the element from the end of the queue, using {$pop: {key:-1}} to remove the element from the beginning of the queue. > db.tianyc03.update ({' name ': ' Xtt '},{$addToSet: {companies:[' C1 ', ' C2 ']}}) > Db.tianyc03.find () {"_id": ObjectId ("50ea6b6f12729d90ce6e341b"), "age": +, "companies": ["Neu", "Alibaba", "Alibaba", "congxing", ["C1", "C2"] ], "name": "Xtt"}> db.tianyc03.update ({' name ': ' Xtt '},{$pop: {companies:1}}) > Db.tianyc03.find () {"_id": ObjectId ("50ea6b6f12729d90ce6e341b"), "age": +, "companies": ["Neu", "Alibaba", "Alibaba", "congxing"], "name": "Xtt"}&gt ;> db.tianyc03.update ({' name ': ' Xtt '},{$pop: {companies:-1}}) > Db.tianyc03.find () {"_id": ObjectId (" 50ea6b6f12729d90ce6e341b ")," age ": +," companies ": [" Alibaba "," Alibaba "," congxing "]," name ":" Xtt "}>6. $pull you can use $pull to remove elements from an array in addition to $pop, you can match the element values to delete all the elements on the match. > db.tianyc03.update ({' name ': ' Xtt '},{$addToSet: {companies: ' Teletek '}}) > Db.tianyc03.find () {"_id": ObjectId ( "50ea6b6f12729d90ce6e341b"), "age": +, "companies": ["Alibaba", "Alibaba", "congxing", "Teletek"], "name": "Xtt"}&G T Db.tianyc03.update ({},{$pull: {companies: ' Congxing '}}) > Db.tianyc03.find () {"_id": ObjectId (" 50ea6b6f12729d90ce6e341b ")," age ": +," companies ": [" Alibaba "," Alibaba "," Teletek "]," name ":" Xtt "}> db.tianyc0 3.update ({},{$pull: {companies: ' Alibaba '}}) > Db.tianyc03.find () {"_id": ObjectId ("50ea6b6f12729d90ce6e341b"), " Age ": +," companies ":["Teletek"], "name": "Xtt"}> 

Transferred from: http://www.cnblogs.com/yuechaotian/archive/2013/02/04/2890266.html

MongoDB Update operation

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.