Summary of commonly used sentences in MongoDB
preface
MongoDB and MySQL basic statements are still very different, today I will introduce some of the commonly used basic statements of MongoDB, the following words do not say much, let's take a look at the detailed introduction
MONgoDB delete statement
delete() Delete
Delete a collection
db.collection.deleteOne()
Delete multiple collections
db.collection.deletMany();
remove() Delete
Delete all name: Li Si's data
db.student.remove({name:"Li Si"});
Delete only one sex: male data delete only one
db.student.remove({sex:"male"},true);
delete all
db.student.remove({});
false deletion of database
Sometimes when the user deletes the operation, the requirement is that only the data is hidden, not really deleted from the database.
At this time, fake deletion was used. For example, this is Zhang Sanfa's two Weibo posts:
db.student.insert([ {name:"Zhang San",content:"Good mood today",isDel:0}, {name:"Zhang San",content:"Average mood today",isDel:0},]);
The user adds two pieces of data, but only keeps the latter one. Delete the former one. In this case, use false deletion. Add a field isDel:0 when adding data.
So when the user deletes data, it executes not the remove method, but the update method.
db.student.update({"_id" : ObjectId("5bd6a46f1eb7a22fa07cb382")},{ $set:{ isDel:1 }});
When isDel:0 is indicating that the user has not deleted, 1 is indicating that the user has deleted.
Therefore, when querying, you need to filter the name and isDel conditions
db.student.find({name:"Zhang San",isDel:0});
Query the data that the user has not deleted:
Then you can fake deletion.
Operation and modification of batch data
Insert 10000 documents into the collection
var arr= [];for(var i=0;i