MongoDB Learning (2): common modification operations

Source: Internet
Author: User
Insert document (insert database) db. person. insert ({_ id: 0001, nameyuexin}) to clear the data db. person. drop () Batch insert document shell does not support batch insert to complete batch insert use for loop (vari0; i10; I ++ ){.. db. persons. insert ({_ id: I, name: yuexin + I })..} save operation: save operation and in

Insert document (insert database) db. person. insert ({_ id: 0001, nameyuexin}) to clear the data db. person. drop () Batch insert document shell does not support batch insert to complete batch insert use for loop (var I = 0; i10; I ++ ){.. db. persons. insert ({_ id: I, name: yuexin + I })..} save operation: save operation and in

Insert document (insert database)
Db. person. insert ({_ id: "0001", name "yuexin "})
Clear Data
Db. person. drop ()
Batch insert documents
Batch insert is not supported in shell.
Batch insert using for Loop
For (var I = 0; I <10; I ++ ){
.. Db. persons. insert ({_ id: I, name: "yuexin" + I })
..}

Save operation
The difference between the save operation and the insert operation is that when the id is the same, the save operation changes to the update operation, and the insert operation reports an error.


Delete all data in the list
Db. persons. remove () delete data in persons but do not delete the index (db. system. indexes. find () has a value)
For
Db. person. drop () will delete the index
Delete
Db. persons. remove ({_ id: "3 "})

If you want to clear a collection with a large amount of data, it is much more efficient to directly Delete the set and re-index it.

1. Tough Document Replacement update operations
Update
> Db. persons. update ({age: 55 },{ name: "000 "})
2. When a primary key conflict occurs, an error is reported and the update operation is stopped.
An error is reported when a document with a hard replacement conflict with an existing document id.
3. insertOrUpdate operation
Objective: To update the data queried by the queryer and replace the data when the data is not found.
Practice:> db. persons. update ({name: "0004" },{ name: "1114"}, true)
4. Batch update
> Db. persons. update ({name: "yuexin" },{$ set: {name: "text" }}, false, true)
By default, the first data entry is modified by default when the queryserver detects multiple data entries.
Db. [documentName]. update ({queryer}, {modifier}, false, true)

Modifier:
$ Set is used to specify a key-value pair. If it exists, it is modified. If it does not exist, it is added.
$ Inc is only used for numeric types. It can add or subtract numeric values corresponding to the specified key.
> Db. persons. update ({age: 20}, {$ inc: {age:-2 }})
$ Unset Delete the specified key
> Db. persons. update ({age: 18}, {$ unset: {age: 1 }})
$ Push
1. If the specified key is a new value appended to the array
> Db. persons. insert ({_ id: 0, name: 0, book: []})
> Db. persons. find ()
{"_ Id": 0, "name": 0, "book": []}
> Db. persons. update ({name: 0}, {$ push: {book: "abc "}})
> Db. persons. find ()
{"_ Id": 0, "book": ["abc"], "name": 0}
> Db. persons. update ({name: 0}, {$ push: {book: "abcd "}})
> Db. persons. update ({name: 0}, {$ push: {book: "abcd "}})
> Db. persons. find ()
{"_ Id": 0, "book": ["abc", "abcd", "abcd"], "name": 0}
2. If the specified key is not an array, the current operation is interrupted.
Cannot apply $ push/$ pushAll modifier to non-array
3. If the specified key does not exist, create an array-type key-value pair.
> Db. persons. update ({name: 0}, {$ push: {things: "abcd "}})
> Db. persons. find ()
{"_ Id": 0, "book": ["abc", "abcd", "abcd"], "name": 0, "things": ["abcd"
]}

$ PushAll is similar to push. It is used to add array data in batches.
> Db. persons. update ({name: 0}, {$ pushAll: {things: ["abcd", "01", "02"]})
> Db. persons. find ()
{"_ Id": 0, "book": ["abc", "abcd", "abcd"], "name": 0, "things": ["abcd"
, "Abcd", "01", "02"]}

$ AddToSet if this option exists in the target array, no operation is performed. If this option does not exist, add it.
> Db. persons. insert ({_ id: 0, name: 0, book: []})
> Db. persons. update ({name: 0}, {$ addToSet: {book: "abcd "}})
> Db. persons. find ()
{"_ Id": 0, "book": ["abcd"], "name": 0}
> Db. persons. update ({name: 0}, {$ addToSet: {book: "abcd "}})
> Db. persons. find ()
{"_ Id": 0, "book": ["abcd"], "name": 0}

$ Pop: Delete the first or last one (-1 is the first and 1 is the last one)
> Db. persons. update ({name: 0}, {$ pop: {book:-1 }})

$ Pull delete a specified
> Db. persons. update ({name: 0}, {$ pull: {book: "01 "}})

$ Pull Delete multiple
> Db. persons. update ({name: 0}, {$ pullAll: {book: ["01", "02"]})

$ Array positioner. If the array has multiple values, we only want to operate some of them. We need to use the locator ($ )(??)

The modifier is placed on the outermost layer, and the queryer is placed on the inner layer.

$ AddToSet combined with $ each to complete batch array update
If yes, no more
> Db. persons. find ()
{"_ Id": 0, "book": ["js"], "name": 0}
> Db. persons. update ({_ id: 0}, {$ addToSet: {book :{$ each: ["js", "db"] }}})
> Db. persons. find ()
{"_ Id": 0, "book": ["js", "db"], "name": 0}

When a document is created, mongoDB allocates memory and reserved memory for it. When the modification operation does not exceed the reserved memory, it is very fast. If the modification operation does not exceed the reserved memory, it will consume time.

RunCommand can execute special functions in mongoDB.
FindAndModify is one of the special functions. It is used to return the document after update or remove.
> Db. persons. find ()
{"_ Id": 0, "book": ["js", "db"], "name": 0}
> Ps = db. runCommand ({
... "FindAndModify": "persons ",
... "Query": {name: 0 },
... "Update": {$ set: {age: 100 }},
... New: true })
{
"LastErrorObject ":{
"UpdatedExisting": true,
"N": 1,
"ConnectionId": 1,
"Err": null,
"OK": 1
},
"Value ":{
"_ Id": 0,
"Age": 100,
"Book ":[
"Js ",
"Db"
],
"Name": 0
},
"OK": 1
}
> Ps. value
{& Quot; _ id & quot;: 0, & quot; age & quot;: 100, & quot; book & quot;: [& quot; js & quot;, & quot; db & quot;], & quot; name & quot;: 0}

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.