
上QQ阅读APP看书,第一时间看更新
The delete operation
MongoDB provides the following methods to remove documents from the collection:
- db.collection.deleteOne();
- db.collection.deleteMany();
The db.collection.deleteMany() method is used to delete all the documents that match the given criteria. If you want to delete all the documents, you can pass an empty document criterion:

This query will delete all the documents contained in the user_profiles collection.
To delete the single most document which matched the given criteria, we use the db.collection.deleteOne() method. The following query deletes the record where the userId is equal to 1:

The following methods can also delete documents from the collection:
- db.collection.findOneAndDelete()
- db.collection.findAndModify()
- db.collection.bulkWrite()