如何删除或删除 MongoDB 中的集合?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30948151/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:28:36  来源:igfitidea点击:

How to drop or delete a collection in MongoDB?

mongodbcollections

提问by fedorqui 'SO stop harming'

What is the best way to drop a collection in MongoDB?

在 MongoDB 中删除集合的最佳方法是什么?

I am using the following:

我正在使用以下内容:

db.collection.drop()

As described in the manual:

手册所述

db.collection.drop()

Removes a collection from the database. The method also removes any indexes associated with the dropped collection. The method provides a wrapper around the drop command.

db.collection.drop()

从数据库中删除一个集合。该方法还会删除与删除的集合关联的任何索引。该方法为 drop 命令提供了一个包装器。

But how can I drop it from the command line?

但是我怎样才能从命令行中删除它呢?

回答by fedorqui 'SO stop harming'

So either of these are valid ways to do it:

因此,以下任一方法都是有效的方法:

mongo <dbname> --eval 'db.<collection>.drop()'
#     ^^^^^^^^            ^^^^^^^^^^^^

db.<collection>.drop()
#  ^^^^^^^^^^^^


For example, for a collection mycollectionin a database mydbyou would say:

例如,对于mycollection数据库中的集合,mydb您会说:

mongo mydb --eval 'db.mycollection.drop()'

db.mycollection.drop()


This is the way I fully tested it, creating a database mydbwith a collection hello.

这是我完全测试它的方式,创建一个mydb带有集合的数据库hello

  • Create db mydb:

    > use mydb
    switched to db mydb
    
  • Create a collection mycollection:

    > db.createCollection("mycollection")
    { "ok" : 1 }
    
  • Show all the collections there:

    > db.getCollectionNames()
    [ "mycollection", "system.indexes" ]
    
  • Insert some dummy data:

    > db.mycollection.insert({'a':'b'})
    WriteResult({ "nInserted" : 1 })
    
  • Make sure it was inserted:

    > db.mycollection.find()
    { "_id" : ObjectId("55849b22317df91febf39fa9"), "a" : "b" }
    
  • Delete the collection and make sure it is not present any more:

    > db.mycollection.drop()
    true
    > db.getCollectionNames()
    [ "system.indexes" ]
    
  • 创建数据库mydb

    > use mydb
    switched to db mydb
    
  • 创建一个集合mycollection

    > db.createCollection("mycollection")
    { "ok" : 1 }
    
  • 在那里显示所有集合:

    > db.getCollectionNames()
    [ "mycollection", "system.indexes" ]
    
  • 插入一些虚拟数据:

    > db.mycollection.insert({'a':'b'})
    WriteResult({ "nInserted" : 1 })
    
  • 确保它已插入:

    > db.mycollection.find()
    { "_id" : ObjectId("55849b22317df91febf39fa9"), "a" : "b" }
    
  • 删除集合并确保它不再存在:

    > db.mycollection.drop()
    true
    > db.getCollectionNames()
    [ "system.indexes" ]
    

This also works (I am not repeating the previous commands, since it is just about recreating the database and the collection):

这也有效(我没有重复前面的命令,因为它只是重新创建数据库和集合):

$ mongo mydb --eval 'db.mycollection.drop()'
MongoDB shell version: 2.6.10
connecting to: mydb
true
$

回答by martinho

To drop a collection 'users' of database 'mydb':

要删除数据库“mydb”的集合“users”:

$ use mydb
$ db.users.drop()