MongoDB: How To Delete All Records Of A Collection in MongoDB Shell?

后端 未结 5 1139
生来不讨喜
生来不讨喜 2021-01-30 15:23

I\'ve tried

db.users.remove(*)

Although it returns an error so how do I go about clearing all records?

5条回答
  •  北恋
    北恋 (楼主)
    2021-01-30 16:14

    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({}); 
        }
    });
    

提交回复
热议问题