1. MongoDB Create databases and collections
The creation of MongoDB databases and collections is implicit. It means that you don't have to write the CREATE DATABASE statement separately. Use the keyword directly. Run under the Bin/mongo Shell:
Use test;
This generates the test database, which is automatically deleted if it is not written away. The collection is also implicit, without specifying it, insert a document directly, producing a collection.
2. Document Writing
Inserting using Insert:
Db.user.insert ({"Name": "Gang"});
User is the collection name, which writes a piece of data.
3. Document deletion
Document removal using the Remove keyword.
Db.user.remove ();
Delete all the data under user. If you specify that you want to delete data for a particular condition, you need to add a parameter to remove.
Db.user.remove ({"Name": "Gang"});
Remove all users with Name gang.
4. Document Modification Update
Document updates using the UPDATE keyword, updates are atomic, and if two updates arrive at the server at the same time, first-to-first execution, and then the execution of the other one. Update has two parameters: first: Query out the document to be updated, the second modifier modifier, and what changes to make.
1. Update updates
Update directly updates the entire document with the second parameter.
Db.user.update ({"Name": "Gang"}, {"New_name": "Gang"});
View Results
Db.user.find (); {"_id": ObjectId ("536f5ccd7a37c2e745770ed7"), "New_name": "Gang"}
2. $set and $unset
$set sets a new value, if it does not exist, is created.
Db.user.update ({"Name": "Gang"}, {"$set": {"Age": 25}});
A new Age option was added, using Find to view
Db.user.find (); {"_id": ObjectId ("536f5ccd7a37c2e745770ed7"), "age": +, "name": "Gang"}
$unset can erase a key
Db.user.update ({"Name": "Gang"}, {"$unset": {"Age": 1}});
View
Db.user.find (); {"_id": ObjectId ("536f5ccd7a37c2e745770ed7"), "name": "Gang"}
3. $inc
$inc is used to increase or decrease the specified value, which is not present when created. Commonly used in counters. $inc must act on integers or floating-point numbers.
Db.user.update ({"Name": "Gang"}, {"$inc": {"Score": 5}});
If you want to reduce it, set it to a negative number.
Db.user.update ({"Name": "Gang"}, {"$inc": {"Score":-5}});
4. Array manipulation
The array operation uses $push to press in one, $pop pops up one bar.
Db.user.update ({"Name": "Gang"}, {"$push": {"subjects": {"Chinese": Ten, "Math": 15}});
With $addtoset processing, duplicate requests are processed, and they do not exist before they are written.
$pop, remove one from the array. Key:1 deletes one from the end of the array. Key:-1 Delete one from the array start.
>db.user.find (); {"_id": ObjectId ("536f5ccd7a37c2e745770ed7"), "name": "Gang", "Age": [1, 2, 3, 4]}>db.user.update ({"Name": "Gan G "}, {" $pop ": {" Age ": 1}}); >db.user.find (); {"_id": ObjectId ("536f5ccd7a37c2e745770ed7"), "Age": [1, 2, 3], "name": "Gang"}> db.user.update ({"Name": "Gang" }, {"$pop": {"Age":-1}});> Db.user.find (); {"_id": ObjectId ("536f5ccd7a37c2e745770ed7"), "Age": [2, 3], "name": "Gang"}
$pull deletes the specified data from the array.
> db.user.update ({"Name": "Gang"}, {"$pull": {"Age": 2}});> Db.user.find (); {"_id": ObjectId ("536f5ccd7a37c2e745770ed7"), "Age": [3], "name": "Gang"}
5. Upsert
Upsert If a match condition is found, the update does not exist and a new one is created. You need to set the third parameter of update to TRUE.
6. Update Multiple documents
Update defaults to updating one record, and if you need to update multiple documents, you need to set the fourth parameter of update to TRUE.
Address: http://blog.csdn.net/yonggang7/article/details/25557563