I've tried
db.users.remove(*)
Although it returns an error so how do I go about clearing all records?
This question is related to
mongodb
To remove all the documents in all the collections:
db.getCollectionNames().forEach( function(collection_name) {
if (collection_name.indexOf("system.") == -1) {
print ( ["Removing: ", db[collection_name].count({}), " documents from ", collection_name].join('') );
db[collection_name].remove({});
}
});
You can delete all the documents from a collection in MongoDB, you can use the following:
db.users.remove({})
Alternatively, you could use the following method as well:
db.users.deleteMany({})
Follow the following MongoDB documentation, for further details.
To remove all documents from a collection, pass an empty filter document
{}
to either thedb.collection.deleteMany()
or thedb.collection.remove()
method.
Delete all documents from a collection in cmd:
cd C:\Program Files\MongoDB\Server\4.2\bin
mongo
use yourdb
db.yourcollection.remove( { } )
db.users.count()
db.users.remove({})
db.users.count()
Source: Stackoverflow.com