1024programmer Java Mongodb write, delete, update

Mongodb write, delete, update

1. Mongodb creates databases and collections The creation of mongodb databases and collections is implicit. This means that there is no need to write a separate create database statement. Just use the use keyword directly. Run under bin/mongo shell: use test; This will generate a test database. If you leave without writing, the system will automatically delete it. Sets are also implicit and do not need to be specifically referred to

1. Mongodb creates databases and collections

Creation of mongodb databases and collections is implicit. This means that there is no need to write a separate create database statement. Just use the use keyword directly. Run under bin/mongo shell:

 use test;

This will generate a test database. If it is not written and left, the system will automatically delete it. Collections are also implicit. You don’t need to specify them. If you directly insert a document, a collection will be generated.

2. Document writing

Insert using insert:

 db.user.insert({"name" : "gang"});

user is the collection name, and a piece of data is written.

3. Document deletion

Document deletion uses the remove keyword.

 db.user.remove();

Delete all data under user. If you specify to delete data under specific conditions, you need to add parameters to remove.

 db.user.remove({"name" : "gang"});

Delete all users named gang.

4. Document modification and update

Document updates use the update keyword, and the updates are atomic. If two updates arrive at the server at the same time, the one that arrives first is executed first, and then the other one is executed. update has two parameters: the first: query the document to be updated, and the second modifier, what modifications to make.

1. update

update can directly update 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 or creates it if it does not exist.

 db.user.update({"name" : "gang"}, {"$set" : {"age" : 25}});

A new age option has been added, use find to view it

 db.user.find();
 { "_id" : ObjectId("536f5ccd7a37c2e745770ed7"), "age" : 25, "name" : "gang" }

$unset can delete a key

 db.user.update({"name" : "gang"}, {"$unset" : {"age" : 1}});

Check

 db.user.find();
 { "_id" : ObjectId("536f5ccd7a37c2e745770ed7"), "name" : "gang" }

3. $inc

$inc is used to increase or decrease the specified value, and create it if it does not exist. Commonly used in counters. $inc must operate 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 operations

Array operations use $push to push an item in and $pop to pop out an item.

 db.user.update({"name" : "gang"}, {"$push" : {"subjects" : {"chinese" : 10, "math" : 15}}});

Use $addToSet to process, repeated requests will be processed, and they will be written only if they do not exist.
$pop, removes one item from the array. key : 1 removes one from the end of the array. key: -1 Delete one from the beginning of the 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 removes 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 is updated if a matching condition is found, and creates a new one if it does not exist. The third parameter of update needs to be set to true.

6. Update multiple documents

Update updates one record by default. If you need to update multiple documents, you need to set the fourth parameter of update to true.

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/mongodb-write-delete-update-2/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

Follow Weibo
Back to top
首页
微信
电话
搜索