Array key for mongodb batch update operation documentation

Source: Internet
Author: User
The data in the persons document is as follows: db. persons. find () {_ id: 2, name: 2} {_ id: 3, name: 3} db. persons. update ({_ id: 4 },{ _ id: 4, name: 4}) WriteResult ({nMatched: 0, nUpserted: 0, nModified: 0}) db. persons. find () {_ id: 2, name: 2} {_ id: 3, n

The data in the persons document is as follows: db. persons. find () {_ id: 2, name: 2} {_ id: 3, name: 3} db. persons. update ({_ id: 4 },{ _ id: 4, name: 4}) WriteResult ({nMatched: 0, nUpserted: 0, nModified: 0}) db. persons. find () {_ id: 2, name: 2} {_ id: 3, n

The data in the persons document is as follows:

> Db. persons. find ()
{"_ Id": 2, "name": 2}
{"_ Id": 3, "name": 3}

> Db. persons. update ({_ id: 4}, {_ id: 4, name: 4 })
WriteResult ({"nMatched": 0, "nUpserted": 0, "nModified": 0 })
> Db. persons. find ()
{"_ Id": 2, "name": 2}
{"_ Id": 3, "name": 3}

After the update operation is completed, the records with _ id: 4 are still invisible, because the update method requires a true indicator to insert records that cannot be queried:

> Db. persons. update ({_ id: 4}, {_ id: 4, name: 4}, true)
WriteResult ({"nMatched": 0, "nUpserted": 1, "nModified": 0, "_ id": 4 })
> Db. persons. find ()
{"_ Id": 2, "name": 2}
{"_ Id": 3, "name": 3}
{"_ Id": 4, "name": 4}

Change the name in the persons document to "3" to "33"

> Db. persons. update ({name: "3" },{$ set: {name: "33" }}, false, true)

False meaning: if the key-value pair name: "33" cannot be found, no insert operation is performed. true Meaning: Batch update

Add the age: "88" attribute to persons

> Db. persons. update ({name: "4" },{ $ set: {age: "88 "}})
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1 })
> Db. persons. find ()
{"_ Id": 2, "name": "33 "}
{"_ Id": 3, "name": "33 "}
{"_ Id": 4, "name": "4", "age": "88 "}

Add age to 2

> Db. persons. update ({name: "4" },{ $ inc: {age: 2 }})
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1 })
> Db. persons. find ()
{"_ Id": 2, "name": "33 "}
{"_ Id": 3, "name": "33 "}
{"_ Id": 4, "name": "4", "age": 90}

Remove the age property:

> Db. persons. update ({age: 90}, {$ unset: {age: 1 }})
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1 })
> Db. persons. find ()
{"_ Id": 2, "name": "33 "}
{"_ Id": 3, "name": "33 "}
{"_ Id": 4, "name": "4 "}

Add a record to the persons document with the _ id of 5

> Db. persons. insert ({_ id: 5, name: 5, books: []})
WriteResult ({"nInserted": 1 })
> Db. persons. find ()
{"_ Id": 2, "name": "33 "}
{"_ Id": 3, "name": "33 "}
{"_ Id": 4, "name": "4 "}
{"_ Id": 5, "name": 5, "books": []}

Add an element to the books array: "js" and "extjs4.0"

> Db. persons. update ({_ id: 5}, {$ push: {books: "js "}})
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1 })
> Db. persons. find ()
{"_ Id": 2, "name": "33 "}
{"_ Id": 3, "name": "33 "}
{"_ Id": 4, "name": "4 "}
{"_ Id": 5, "name": 5, "books": ["js"]}

> Db. persons. update ({_ id: 5}, {$ push: {books: "extjs4.0 "}})
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1 })
> Db. persons. find ()
{"_ Id": 2, "name": "33 "}
{"_ Id": 3, "name": "33 "}
{"_ Id": 4, "name": "4 "}
{"_ Id": 5, "name": 5, "books": ["js", "extjs4.0"]}

Create a new classes array:

> Db. persons. update ({_ id: 5}, {$ push: {classes: "01 class "}})
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1 })
> Db. persons. find ()
{"_ Id": 2, "name": "33 "}
{"_ Id": 3, "name": "33 "}
{"_ Id": 4, "name": "4 "}
{"_ Id": 5, "name": 5, "books": ["js", "extjs4.0"], "classes": ["01 class"
]}

Add several elements to the calsses array at a time:

> Db. persons. update ({_ id: 5}, {$ pushAll: {classes: ["02 class", "03 class", "04 class"]}
)
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1 })
> Db. persons. find ()
{"_ Id": 2, "name": "33 "}
{"_ Id": 3, "name": "33 "}
{"_ Id": 4, "name": "4 "}
{"_ Id": 5, "name": 5, "books": ["js", "extjs4.0"], "classes": ["01 class"
, "02 class", "03 class", "04 class"]}

Delete a record whose _ id is 5 and create a record whose _ id is 5. Use $ addToSet to check whether the element to be added exists in the array, if it exists, it will not be added; otherwise it will be added:

> Db. persons. remove ({_ id: 5 })
WriteResult ({"nRemoved": 1 })
> Db. persons. find ()
{"_ Id": 2, "name": "33 "}
{"_ Id": 3, "name": "33 "}
{"_ Id": 4, "name": "4 "}
> Db. persons. insert ({_ id: 5, books: ["js"]})
WriteResult ({"nInserted": 1 })
> Db. persons. find ()
{"_ Id": 2, "name": "33 "}
{"_ Id": 3, "name": "33 "}
{"_ Id": 4, "name": "4 "}
{"_ Id": 5, "books": ["js"]}
> Db. persons. update ({_ id: 5}, {$ addToSet: {books: "js "}})
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 0 })
> Db. persons. find ()
{"_ Id": 2, "name": "33 "}
{"_ Id": 3, "name": "33 "}
{"_ Id": 4, "name": "4 "}
{"_ Id": 5, "books": ["js"]}
> Db. persons. update ({_ id: 5}, {$ addToSet: {books: "java "}})
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1 })
> Db. persons. find ()
{"_ Id": 2, "name": "33 "}
{"_ Id": 3, "name": "33 "}
{"_ Id": 4, "name": "4 "}
{"_ Id": 5, "books": ["js", "java"]}
> Db. persons. update ({_ id: 5 },{$ addToSet: {books: "mongo "}})
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1 })
> Db. persons. find ()
{"_ Id": 2, "name": "33 "}
{"_ Id": 3, "name": "33 "}
{"_ Id": 4, "name": "4 "}
{"_ Id": 5, "books": ["js", "java", "mongo"]}

Delete the first element of the books array: "js". Run the $ pop command:

> Db. persons. update ({_ id: 5 },{ $ pop: {books:-1 }})
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1 })
> Db. persons. find ()
{"_ Id": 2, "name": "33 "}
{"_ Id": 3, "name": "33 "}
{"_ Id": 4, "name": "4 "}
{"_ Id": 5, "books": ["java", "mongo"]}
> Db. persons. update ({_ id: 5 },{ $ pop: {books: 1 }})
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1 })
> Db. persons. find ()
{"_ Id": 2, "name": "33 "}
{"_ Id": 3, "name": "33 "}
{"_ Id": 4, "name": "4 "}
{"_ Id": 5, "books": ["java"]}

-1 indicates the first element, and 1 indicates the last element.

You can also use the pull command to delete a specified element at a time:

> Db. persons. find ()
{"_ Id": 2, "name": "33 "}
{"_ Id": 3, "name": "33 "}
{"_ Id": 4, "name": "4 "}
{"_ Id": 5, "books": ["java", "mongo", "js"]}
> Db. persons. update ({_ id: 5}, {$ pull: {books: "js "}})
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1 })
> Db. persons. find ()
{"_ Id": 2, "name": "33 "}
{"_ Id": 3, "name": "33 "}
{"_ Id": 4, "name": "4 "}
{"_ Id": 5, "books": ["java", "mongo"]}

The $ pullAll command can specify multiple elements to be deleted at a time:

> Db. persons. update ({_ id: 5}, {$ pullAll: {books: ["java", "mongo"]})
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1 })
> Db. persons. find ()
{"_ Id": 2, "name": "33 "}
{"_ Id": 3, "name": "33 "}
{"_ Id": 4, "name": "4 "}
{"_ Id": 5, "books": []}

Create a new record named _ id 6:

> Db. persons. find ()
{"_ Id": 2, "name": "33 "}
{"_ Id": 3, "name": "33 "}
{"_ Id": 4, "name": "4 "}
{"_ Id": 5, "books": []}
{"_ Id": 6, "books": [{"type": "js", "name": "extjs4.0" },{ "type": "db"
, "Name": "mongodb" },{ "type": "js", "name": "jquery"}]}

Add the pens: "too long" attribute to the books element whose type is js. The. symbol must be referenced using double quotation marks.

> Db. persons. update ({"books. type": "js" },{$ set: {"books. $. author": "tom "}})
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1 })
> Db. persons. find ()
{"_ Id": 2, "name": "33 "}
{"_ Id": 3, "name": "33 "}
{"_ Id": 4, "name": "4 "}
{"_ Id": 5, "books": []}
{"_ Id": 6, "books": [{"type": "js", "name": "extjs4.0", "author": "tom"
},{ "Type": "db", "name": "mongodb"}]
}

Db. persons. update ({"books. type": "js" },{$ set: {"books. $. pens": "too long "}})

When an array element is inserted, repeated elements are not inserted for the second time:

> Db. persons. find ()
{"_ Id": 2, "name": "33 "}
{"_ Id": 3, "name": "33 "}
{"_ Id": 4}
{"_ Id": 5, "books": ["js"]}

> Db. persons. update ({_ id: 5 },{$ addToSet: {books: {$ each: ["js", "db", "java"] }})
WriteResult ({"nMatched": 1, "nUpserted": 0, "nModified": 1 })
> Db. persons. find ()
{"_ Id": 2, "name": "33 "}
{"_ Id": 3, "name": "33 "}
{"_ Id": 4}
{"_ Id": 5, "books": ["js", "db", "java"]}

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.