How to delete selected multiple records in a collection in MongoDB using MongoDB compass

拥有回忆 提交于 2021-01-02 05:37:09

问题


I'm very new to MongoDB and MongoDB Compass.

I have some 1000 records in a customer collection. How I can delete all the records at once through MongoDB compass.

Many Thanks,


回答1:


If you don't have indexes on that collection you can just drop the entire collection as shown in the docs.

If you have indexes, however, dropping the collection will also delete the indexes, but it is usually much faster to drop the whole thing and then recreate both the collection and the indexes than removing all the documents using remove (db.collection.remove({})), as that would have to go document by document freeing up the space they take and removing their entry on the indexes, if any.

The only caveat of this approach is that in a sharded cluster, you might drop a collection and, even if the drop is reported as successful, the collection might still be present in some nodes, causing issues when recreating it.

You can read more about this still OPEN issue and its workaround at https://jira.mongodb.org/browse/SERVER-17397:

ISSUE SUMMARY

When dropping a database / collection in a sharded cluster, even if the drop is reported as successful it is possible the database / collection may still be present in some nodes in the cluster. At this time, we do not recommend that users drop a database or collection and then attempt to reuse the namespace.

USER IMPACT

When the database/collection is not successfully dropped in a given node, the corresponding files continue to use disk space in that node. Attempting to reuse the namespace may lead to undefined behavior.

WORKAROUNDS

To work around this issue one can follow the steps below to drop a database/collection in a sharded environment:

  1. Drop the database / collection using a mongos.
  2. Connect to each shard's primary and verify the namespace has been dropped. If it has not, please drop it. Dropping a database (e.g db.dropDatabase()) removes the data files on disk for the database being dropped.
  3. Connect to a mongos, switch to the config database and remove any reference to the removed namespace from the collections chunks, locks, databases and collections:

    When dropping a database:

    use config
    db.collections.remove( { _id: /^DATABASE\./ } )
    db.databases.remove( { _id: "DATABASE" } )
    db.chunks.remove( { ns: /^DATABASE\./ } )
    db.locks.remove( { _id: /^DATABASE\./ } )
    

    When dropping a collection:

    use config
    db.collections.remove( { _id: "DATABASE.COLLECTION" } )
    db.chunks.remove( { ns: "DATABASE.COLLECTION" } )
    db.locks.remove( { _id: "DATABASE.COLLECTION" } )
    
  4. Connect to each mongos and run flushRouterConfig.




回答2:


You can use Embedded Shell provided by the MongoDB compass. Its in beta mode right now. I am using compass version 1.23.0

To delete multiple records, you can use db.<collectionname>.deleteMany
https://docs.mongodb.com/manual/reference/method/db.collection.deleteMany/



来源:https://stackoverflow.com/questions/50448957/how-to-delete-selected-multiple-records-in-a-collection-in-mongodb-using-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!