I just started to check on the Internet how to use momgoose to delete all documents in the collection. But I didn’t find it. I just blogged it down. It’s convenient for myself and others.
It is also possible to use the native statements in mongoDB. But in line with the requirements of the code to be written, it is really unwilling to write this way.
How to use the methods provided by mongoose to delete all documents in the collection in mongoose is listed below.
Code:
They are: Model.remove(Delete document condition, callback function)
.
Example:
// delete all documents // Student is a Model. Student.remove((), function ( err ) { // If the filter condition is empty, it means all console .log( "success" ); });
It can also be like this:
// delete all documents // Student is a Model. Student.remove( function ( err ) { console .log( "success" ); });
The above two methods are related use cases can be found in the official documentation and StackOverFlow.
But my Mongoose version is 5.9.Officially, the above method is a deprecated method. Do not use it anymore. We recommend using another method instead.
Method after 5.9: Model.deleteMany()
// Student is a Model. Student.deleteMany({}, function ( err ) { console .log( "success" ); });
Read More:
- [Mybatis] How to Solve the problem of Oracle query processing more than 1000 in conditions
- [Solved] no all patern found! & Error on Generate Activation Code…
- Redis Delete dump.rdb Mistakenly [How to Solve]
- How to Check the most CPU consuming 50 queries in MYSQL
- ERROR 1261 (01000): Row 1 doesn‘t contain data for all columns
- Solve MySQL table can not query, modify, delete and other operations and appear stuck
- How to quickly locate the problem in DB2-407 error reporting SQLCODE: -407, SQLSTATE: 23502
- [Solved]Error 1054 (42s22): unknown column ‘password’ in ‘field list’ how to modify the password
- MySQL: How to Turn on Functions
- How to Solve mysql [Err] 1067-Invalid default value for
- How to Solve MYSQL into outfile Error13
- How to Fix MySQL ERROR 1130 (HY000): Host ‘XXXX’ is not allowed to connect to this MySQL server
- How to Solve SQL state [HY000]: general error 2503
- How to Solved Error: 18456, Severity: 14, State: 38.
- How to Solve Azkaban startup error
- How to Solve Mysql8 load data error
- MYSQL: How to Use ifnull()
- Docker: How to Solve MYSQL8 & Navicat remote connection error
- How to Solve Error: Rsa Public Key not Find
- How To Change ASM SYS PASSWORD