MongoDB Learning (ii) common modification operations

Source: Internet
Author: User
Tags bulk insert modifier

Inserting a document (inserting a database)
Db.person.insert ({_id: "0001", Name "Yuexin"})
Clear Data
Db.person.drop ()
Bulk INSERT Document
Bulk INSERT not supported in shell
Complete BULK INSERT using for loop
for (Var i=0;i<10;i++) {
.. Db.persons.insert ({_id:i,name: "Yuexin" +i})
.. }


Save action
The difference between the save operation and the insert operation is that when the ID is the same, save changes to the update operation and insert will error


Delete all data in a list
Db.persons.remove () deletes data in persons but does not delete the index (there is a value in Db.system.indexes.find ())
For
Db.person.drop () will delete the index
Delete with query criteria.
Db.persons.remove ({_id: "3"})


If you want to clear a collection with a very large amount of data, it is much more efficient to delete the collection directly and re-index it than to use remove directly.


1. Tough document-replacement update operation
Update updates
> db.persons.update ({age:55},{name: "000"})
2. Primary key conflict when error and stop update operation
Because it is hard to replace a document with an existing document ID conflict when an error occurs
3.insertOrUpdate operation
Purpose: The query query out the data to perform the update operation, can not find out the replacement operation
Procedure:> 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 is modified by default when the query detects more than one piece of data.
Db. [Documentname].update ({finder},{modifier},false,true)


Modifier:
$set is used to specify a key-value pair that, if present, is modified without being added
$inc is only used for numeric types, he can add and subtract values for numeric types that correspond 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 an array append a new value
> 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. Creates a key value pair for an array type if the specified key does not exist
> db.persons.update ({name:0},{$push: {things: "ABCD"}})
> Db.persons.find ()
{"_id": 0, "book": ["abc", "ABCD", "ABCD"], "name": 0, "things": ["ABCD"
] }


$pushAll similar to push, is to batch-join array data
> 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 the target array exists, it does not operate, and does not exist.
> 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 (1 is the first, 1 is the last)
> db.persons.update ({name:0},{$pop: {book:-1}})


$pull Delete the specified
> db.persons.update ({name:0},{$pull: {book: "01"}})




$pull Delete Multiple
> db.persons.update ({name:0},{$pullAll: {book:["01", "02"]})


$ array Locator, if the array has multiple values we just want to operate on one part of it we need to use the locator ($) (?? )


The modifier is placed on the outermost, the Finder is placed on the inner layer of the


Batch array update $addToSet combined with $each
Some words don't add.
> 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 the document is created, MongoDB allocates memory and reserves memory, and when the modification operation does not exceed the reserved memory, it is very fast, instead of allocating new memory, it consumes time.

RunCommand can execute special functions in MongoDB
Findandmodify is one of the special functions he 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
{"_id": 0, "age": +, "book": ["JS", "DB"], "name": 0}

MongoDB Learning (ii) common modification operations

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.