Mongodb writes, deletes, and updates

Source: Internet
Author: User
Tags mongo shell
1. mongodb creates a database and a collection of mongodb databases and sets implicitly. This means that you do not need to write the createdatabase statement separately. Use the use keyword directly. Run: usetest; In binaryshell to generate the test database. If no data is written or left, the system deletes the database automatically. The set is also implicit.

1. mongodb creates a database and a collection of mongodb databases and sets implicitly. This means that you do not need to write the create database statement separately. Use the use keyword directly. Run: use test; in bin/mongo shell to generate the test database. If no data is written and left, the system automatically deletes the database. The set is also implicit.

1. mongodb creates a database and a collection of mongodb databases and sets implicitly. This means that you do not need to write the create database statement separately. Use the use keyword directly. Run in bin/mongo shell:
use test;
This will generate the test database. If the database is not written or left, the system will delete it automatically. The set is also implicit. If you insert a document directly without specifying it, a set is generated. 2. insert into the document:
db.user.insert({"name" : "gang"});
User is the set name, which writes a piece of data. 3. delete a document use the remove keyword to delete a document.
db.user.remove();
Delete all data under a user. If you specify to delete data under a specific condition, you must add a parameter to remove.
db.user.remove({"name" : "gang"});
Delete all users whose name is gang. 4. modify and update a document update use the update keyword. The update is atomic. If two updates arrive at the server at the same time, they are first executed and then executed. Update has two parameters: first, query the document to be updated, and the second modifier. 1. update: You can use the second parameter to update the entire document.
db.user.update({"name" : "gang"}, {"new_name" : "gang"});
View results
db.user.find();{ "_id" : ObjectId("536f5ccd7a37c2e745770ed7"), "new_name" : "gang" }
2. set $ set and $ unset $ set to set a new value. If it does not exist, it is created.
db.user.update({"name" : "gang"}, {"$set" : {"age" : 25}});
A new age option is added and can be viewed using find.
db.user.find();{ "_id" : ObjectId("536f5ccd7a37c2e745770ed7"), "age" : 25, "name" : "gang" }
$ Unset can delete a key.
db.user.update({"name" : "gang"}, {"$unset" : {"age" : 1}});
View
db.user.find();{ "_id" : ObjectId("536f5ccd7a37c2e745770ed7"), "name" : "gang" }
3. $ inc is used to increase or decrease the specified value. If no value exists, it is 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 the number, set it to a negative number.
db.user.update({"name" : "gang"}, {"$inc" : {"score" : -5}});
4. Use $ push to push an array operation and $ pop to bring up one.
db.user.update({"name" : "gang"}, {"$push" : {"subjects" : {"chinese" : 10, "math" : 15}}});
When $ addToSet is used for processing, repeated requests are processed and data is written only if the request does not exist.
$ Pop: delete one from the array. Key: 1 delete one from the end of the array. Key:-1 delete an array.
 >db.user.find();{ "_id" : ObjectId("536f5ccd7a37c2e745770ed7"), "name" : "gang", "age" : [ 1, 2, 3, 4 ] }>db.user.update({"name" : "gang"}, {"$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. upsertupsert: If a matching condition is found, it is updated. If no matching condition exists, a new one is created. Set the third parameter of update to true. 6. update multiple documents by default, update a record. To update multiple documents, set the fourth parameter of update to true.

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.