This is the 18th day of my participation in Gwen Challenge

For the backend god, the daily work is CRUD, plus Control + C and Control + V. As a god, how can you not understand CRUD? MongoDB CRUD is simpler and more modern than SQL statements.

CREATE data

MongoDB provides two ways to create data:

db.crud.insert({name: Farmers' of ', gender: 'male'});
db.crud.save({name: 'Island code farmer', gender: 'male'});
Copy the code

The save method differs in that it updates the corresponding data if it carries the _ID attribute, and inserts new data otherwise. Two insertion methods have been added since MongoDB 3.2: insertOne and insertMany, and the insert method has been marked obsolete.

db.crud.insertOne({name: Farmers' of ', gender: 'male'});
db.crud.insertMany([{name: 'Island code farmer', gender: 'male'},{name: Program girl, gender: 'woman'}]);
Copy the code

Update data

When updating, the query matching condition is preceded by the data to be updated.

# Give a non denaturation
db.crud.update({name: Farmers' of '}, {name: Farmers' of ', gender: 'woman'});
Copy the code

The default value of the update method is multi: true. The default value of the update method is multi: true. Also, note that the document is completely replaced with new data.

# Denaturate all codes
db.crud.update({name: Farmers' of '}, {name: Farmers' of ', gender: 'woman'}, {multi: true});
Copy the code

In MongoDB 3.2, updateOne and updateMany methods are added to updateOne and more data, respectively.

# Restore the gender of the coder
db.crud.updateOne({name: Farmers' of '}, {$set: {name: 'Island code farmer', gender: 'male'}});
db.crud.updateMany({name: Farmers' of '}, {$set: {name: 'Island code farmer', gender: 'male'}});
Copy the code

In the new version of MongoDB, updateOne and updateMany are required to be atomic operations, that is, $set must be used to specify updated fields to prevent misoperations from overwriting the entire document. The Update Operation Document must contain atomic Operators. ** We recommend using ****updateOne** and **updateMany** for more security and clarity. If the document needs to be replaced, you can use replaceOne:

db.crud.replaceOne({name: 'Island code farmer'}, {name: Program girl, gender:'woman'});
Copy the code

DELETE

After version 3.2, MongoDB can be deleted using deleteOne and deleteMany. One or more matching data can be deleted.

db.crud.deleteOne({name: Program girl});
db.crud.deleteMany({gender: 'woman'});
Copy the code

In earlier versions, the remove method was used, and remove if the second argument is true means that only one matching piece of data is removed.

db.crud.remove({name: Program girl});
db.crud.remove({gender: 'woman'}, true);
Copy the code

Note that if the remove method is empty, all data will be deleted, which will cause the library to run.

# Careful operation, beware of deleting library run away
db.crud.remove({});
Copy the code

READ data (READ)

The data is read using the find or findOne methods, where find returns all results, or you can limit the number of returns.

Select * from * where * from *
db.crud.find();
Return only 2 pieces of data
db.crud.find().limit(2);
Select * from Tom where name = 'Tom'
db.crud.find({name: 'Tom'});
Copy the code

If you need to beautify the return result, you can use the pretty() method.

db.crud.find().limit(2).pretty();
Copy the code

Cannot do exclusion on field gender in inclusion projection Cannot do exclusion on field gender in inclusion projection

# return only the _id and name fields
db.crud.find({name: 'Tom'}, {name: 1});
# do not return _id
db.crud.find({name: 'Tom'}, {_id: 0, name: 1});
Copy the code

conclusion

This article covered the basic CRUD operations of MongoDB. As you can see, MongoDB’s syntax is extremely concise and can be operated using chain calls. The parameters of the operation are fixed and the method names are clear, making it easy to get started.