MongoDB Document Operations

Source: Internet
Author: User
Tags modifier

Database operation is nothing more than to increase, delete, change, check. This article mainly introduces adding, deleting and changing.

1. Increase

The MongoDB insert operation is simple, using the keyword "Insert". Instance:

1> Db.test.blog.insert ({"haha": "Xiaohaha", "abc": "123"})  2>Db.test.blog.find (); 3{"_id": ObjectId ("5334dd149b7a445ea2166559"), "title": "Love", "Content": "I4Love You " }  5{"_id": ObjectId ("5334dd669b7a445ea216655a"), "job": "Java", "City": "Wuhan"6  }  7{"_id": ObjectId ("5334ec3f9b7a445ea216655b"), "a": "B" }  8{"_id": ObjectId ("5338bc89125a366c3f32dd00"), "haha": "Xiaohaha" }  9{"_id": ObjectId ("5338bce3125a366c3f32dd01"), "haha": "Xiaohaha", "abc": " ATen3 " }   One>

Use the Insert keyword to set the document inside. If there is no "_id" in the document, MongoDB is automatically generated.

When the insert operation is performed, the database verifies that the "_id" key is included and that the document does not exceed 4MB, except that no additional checks are made. This makes the database more secure and away from injection attacks (MongoDB does not execute code when inserting).

2. By deleting
1 > Db.test.blog.remove ({"A": "B"})  2 > Db.test.blog.find ({"A": "B"})   3 > Db.test.blog.remove ()  

Remove can accept a query document as an optional parameter, and after a given parameter, only documents that meet the criteria will be deleted. If there is no parameter, all documents in the collection are deleted, but the collection itself is not deleted, and the original index is retained.

3. Change

After the document is in the database, you can use the Update method to modify it, and update has two parameters, one for querying the document, for finding the document to update, and the other is the modifier document, which describes what changes are made to the found document. When the update operation is atomic, if two updates occur at the same time, the server first executes, then the other executes.

Update mode one: Document structure changes relatively large use
1>varJobs = Db.test.blog.findOne ({"Job": "Java"});//here, you can query a single object for later use of update modifications2> jobs.job = "Hadoop"; 3 Hadoop4> jobs.exp = "5 Year"; 55 Year6> jobs.city = ["Wuhan", "Beijin", "Shanghai"]; 7["Wuhan", "Beijin", "Shanghai" ]  8> db.test.blog.update ({"Job": "Java"},jobs);//Modify with Update9> Db.test.blog.find ({"Job": "Hadoop"}); Ten{"_id": ObjectId ("5334dd669b7a445ea216655a"), "job": "Hadoop", "City": ["Wuhan", "Beijin", "Shanghai"], "exp": "5 y Ear "}
Modify Data Mode one codeUpdate mode two: Use the modifier, the document only makes a partial update use
    • $set and $unset

$set the value used to specify a key. If the key does not exist, it is created. Can be used to modify values, arrays, inline documents.

$unset used to unload a key-value pair.

1> Db.test.blog.insert ({"_id": 0001, "name": "Joe", "age": +, "Favortie": "book"});//Inserting Data2> Db.test.blog.find ();//Enquiry3{"_id": 1, "name": "Joe", "age": +, "Favortie": "Book" }  4> db.test.blog.update ({"Name": "Joe"},//Update individual data age, and change data type int->string5... {"$set": {"Age": "29"}})  6>Db.test.blog.find (); 7{"_id": 1, "Age": "$", "Favortie": "book", "Name": "Joe" }  8> db.test.blog.update ({"Name": "Joe"},//into an array9... {"$set": {"Favortie": ["book", "TV"]}})  Ten>Db.test.blog.find ();  One{"_id": 1, "Age": "$", "Favortie": ["book", "TV"], "name": "Joe"}

$unset code

1 > db.test.blog.find ();   2 {"_id": 1, "Age": "$", "Favortie": ["book", "TV"], "name": "Joe" }  3 > Db.test . blog.update ({"Name": "Joe"},  4 ... {"$unset": {"Age": 1}})   5 > db.test.blog.find ();   6 {"_id": 1, "Favortie": ["book", "TV"], "name": "Joe"}  

Update numbers

$inc used to increase the key value, the key does not exist when creating

1 > Db.test.blog.update ({"Name": "Joe"},{"$inc": {"Age": 1}});   2 > db.test.blog.find ();   3

Array modifiers

$push will add an element to the end of an existing array, and if not, create an array

1 > db.test.blog.find ();   2 {"_id": 1, "Age": 1, "like": ["Eat", "Booke"], "name": "Joe" }  3 > Db.test.blog . Update ({"name": "Joe"},{$push: {"like": "ABC"}});   4 > db.test.blog.find ();   5

$pop can delete an element from either end of the array. {$pop: {key:1}} is removed from the end of the array, {$pop: {key:1}} is removed from the head

$pull Delete based on specific criteria

1 > Db.test.blog.update ({"Name": "Joe"},{$pull: {"like": "Booke"}});   2 > db.test.blog.find ();   3 {"_id": 1, "Age": 1, "like": ["Eat", "abc"], "name": "Joe"}

Array positioning

Array positioning can use position or position operator "$"

Array subscripts start at 0, so you can use subscripts to select elements directly as keys

Using Upsert (UPDATE or insert)

Upsert is a special update that creates a new document based on this condition and updating the document, if no document meets the update requirements. If a matching document is found, it is updated normally. The third parameter of update indicates whether to use the Upsert update

1> Db.test.blog.find ()//Querying all data2{"_id": 1, "Age": 1, "like": ["Eat", "abc"], "name": "Joe"}//no data that I want to update3> db.test.blog.update ({"url": "Www.baidu.com"},{"$inc": {"visits": 1}},true)//modify using the Upsert method4> Db.test.blog.find ()//query all data and find one more document5{"_id": 1, "Age": 1, "like": ["Eat", "abc"], "name": "Joe" }  6{"_id": ObjectId ("533a1c27b653a97435a02030"), "url": "Www.baidu.com", "visits7": 1 }  8> db.test.blog.update ({"url": "Www.baidu.com"},{"$inc": {"visits": 1}},true)//Use Upsert again9>Db.test.blog.find ()Ten{"_id": 1, "Age": 1, "like": ["Eat", "abc"], "name": "Joe"}//discovery is modifying the document One{"_id": ObjectId ("533a1c27b653a97435a02030"), "url": "Www.baidu.com", "visits A": 2}

Update multiple documents

By default, updates only modify the first document that meets the criteria, and if there are multiple documents that match the criteria, the remaining documents do not change. If you want to update all documents that match the criteria, set the fourth parameter to True

1> db.test.blog.find ({"url": "Www.baidu.com"});//the query URL is Baidu's record has two2{"_id": ObjectId ("533a1c27b653a97435a02030"), "url": "Www.baidu.com", "visits3": 2 }  4{"_id": "2", "url": "Www.baidu.com", "Visits": "5" }  5  //Update all eligible data6> db.test.blog.update ({"url": "Www.baidu.com"},{"$set": {"visits": 10}},false,true)  7   8> db.test.blog.find ({"url": "Www.baidu.com"});//re-query, the data has been modified9{"_id": ObjectId ("533a1c27b653a97435a02030"), "url": "Www.baidu.com", "visitsTen": 10 }   One{"_id": "2", "url": "Www.baidu.com", "Visits": 10}

The four parameter descriptions for update:

1. Need to update the criteria for document matching

2. Data that needs to be updated

3. Whether to update with Upsert: Default False

4. Whether documents with matching criteria are modified: false by default

MongoDB Document 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.