MongoDB entry-add, delete, modify, and query-mongodb entry-add, delete

Source: Internet
Author: User
Tags mongodb client

MongoDB entry-add, delete, modify, and query-mongodb entry-add, delete

Mongodb has been successfully started in the previous blog: continue. Open another cmd and enter the [mongo] command to open the shell, that is, the mongodb client. The "test" database is connected by default. Here I set the Set (table) as student. Figure 1:



1. Add insert


Syntax: db. Set. insert ({"Col1": "column value 1", "Col2": "column value 2 ",...," Coln ":" column value n "})


2. find
2.1 All queries


Syntax: db. Set. find ()


2.2 conditional Query


Syntax: db. Set. find ({"Col1": "column value 1", "Col2": "column value 2 ",...," Coln ":" column value n "})


Operation example: Add + search



 
Note: The "_ id" field is the GUID that the database uses by default to ensure data uniqueness.


3. Modify
3.1 modify update all


Syntax: db. Set. update ({"Col": "column value" },{ "Col2": "column value 2 ",...," Coln ":" column value n "})


Note:


First parameter {...} Is the "Search Condition", the second parameter {...} Is the value to be updated ";
Update () is changed to all. The second parameter must contain all "fields". Otherwise, data will be lost after the Update. For details about the impact, see the example.


Operation example: modify all + error instances



3. 2. Local Modification


Mongodb provides two modifiers for local modifications: $ inc and $ set.


① $ Inc Modifier
$ Inc is short for increase. It is a value specified by auto-increment $ inc. If this key is not found in the document, a key is created.

Syntax: db. set. update ({"col1": "column value 1"}, {$ inc: {"col2": "column value 2 ",..., "coln": "column value n "}}


② $ Set Modifier

Replace the corresponding data with the value specified by $ set.

Syntax: db. set. update ({"col1": "column value 1"}, {$ set: {"col2": "column value 2 ",..., "coln": "column value n "}})


Operation example: local modification + advanced Modification



3.3 upsert operation


Upsert = update + insert: intelligently identifies updates or additions, so I prefer to call it advanced modifications. That is to say, if the document is found to be updated directly, otherwise a new one will be added to the database. Set the third parameter of update to true. For operation examples, see.


Syntax: db. Set. update ({"Col": "column value" },{ $ inc: {"Col2": "column value 2 ",..., "Coln": "column value n" }}, true)


3.4 batch Modification


Improvements should be made on the basis of the previous several, rather than explanations.
Syntax: db. Set. update ({"Col": "column value" },{ $ inc: {"Col2": "column value 2 ",..., "Coln": "column value n" }}, true, true)


Operation example: Batch Modification


4. Delete remove


Syntax: db. Set. remove ()
Db. Set. remove ({"Col": "column value" },{ "Col2": "column value 2 ",..., "Coln": "column value n "})


Operation example: Delete


(5 ~ 6. Advanced operations are not described in detail)


5. query groups by group


Operation example: Group Search


6. distinct Removal
7. count statistics


Operation example: deduplication + statistics


The above is the instance sharing after my operation. After I perform the operation again, I feel more gains. I still remember that when I first started the operation, I only touched on all the modifications. At that time, I thought that the modification was an update () method just like other database operations, therefore, some modifications were made directly in practice, resulting in data loss as shown in the figure. At that time, I was still thinking, "No, do you have to write all the changes to only one or two fields? This is too much trouble ...", Later, I discussed with Dr. Zhou Xiang next to him that he told me that there are two ways to modify mongodb: The specific modification methods encapsulated by mongodb mentioned above. At that time, it seemed to have taken a detour. But now, do you think it is actually a detour?







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.