MongoDB has successfully started in the previous post: http://blog.csdn.net/u010773667/article/details/41847487, it's time to do a series of operations. We open a cmd, enter the "MONGO" command to open the shell is MongoDB client, the default connection is the "test" database, I set the collection (table) is student. Figure One:
1. Add insert
Syntax: DB. Insert ({"Col1": "Column value 1", "Col2": "Column value 2",..., "Coln": "Column Value n"})
2. Finding find
2.1 All Queries
Syntax: DB. Collection. Find ()
2.2 Item Query
Syntax: DB. Set. Find ({"Col1": "Column value 1", "Col2": "Column value 2",..., "Coln": "Column Value n"})
Action Example: Add + Find
Note: the "_id" field is the GUID that the database gives us by default to ensure the uniqueness of the data.
3. Modifications
3.1 Modify Update all
Syntax: DB. Set. Update ({"Col": "Column Value"},{"Col2": "Column value 2",..., "Coln": "Column Value n"})
Attention:
First parameter {...} For "condition found", the second parameter {...} is the value to update;
Update () is all modified, the second parameter needs to contain all the "fields", otherwise the data will be lost after the update, the specific effects see examples.
Action Example: all modifications + Error instances
3.2. Local modifications
MongoDB provides two modifiers for local modifications: $inc and $set.
① $inc modifier
$INC is the abbreviation for increase, which $inc the specified value, and if the key is not in the document, the 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 directly with the value specified by $set.
Syntax: DB. Set. Update ({"col1": "Column value 1"},{$set: {"col2": "Column value 2",..., "Coln": "Column Value n"}})
Action Example: Local modification + advanced modification
3.3 Upsert operation
Upsert=update+insert, intelligent Judgment Update or add, so I prefer to call it advanced modification. In other words: If the document is updated directly, then a new one is added to the database. Set the third parameter of update to TRUE. See the operating example.
Syntax: DB. Collection. Update ({"Col": "Column Value"},{$inc: {"Col2": "Column value 2",..., "Coln": "Column value n"}},true)
3.4 Batch Modification
On the basis of the previous several improvements, not to do more explanation.
Syntax: DB. Collection. Update ({"Col": "Column Value"},{$inc: {"Col2": "Column value 2",..., "Coln": "Column value n"}},true,true)
Action Example: Batch modification
4. Delete Remove
Syntax: DB. Collection. Remove ()
Db. Set. Remove ({"Col": "Column Value"},{"Col2": "Column value 2",..., "Coln": "Column Value n"})
Action Example: delete
(5~6 for advanced operation, not detailed introduction)
5. Group Query Groups
Action Example: grouping lookups
6. Except for heavy distinct
7. Statistics Count
Action example: except for weight + statistics
The above is my operation after the instance sharing, again operation again feel the harvest more. Remember the beginning of the operation of the time, about the modification part only touched all the changes, at that time also thought with the other database operations as modified is an update () method, so the practice of the time directly made part of the modification, the consequences as shown in the picture caused by the loss of data. I was thinking, "No, just change one or two fields and write all of them?" This is too much trouble ... "And later with the big God next to the brother Siang discussion he told me MongoDB modifies there are two methods that are mentioned above for MongoDB specifically encapsulated partial modification method. At that time seemed to have gone a detour, but now think really is a detour?
MongoDB Introduction--Adding and deleting changes