如何在 MongoDB 中删除集合?
mongodbdatabasebig data analytics
要在 MongoDB 中删除集合,您需要使用 drop() 命令。语法如下:
db.yourCollectionName.drop();
上述语法返回 true 或 false。如果集合删除成功,则返回 true,否则返回 false。
首先,让我们显示 MongoDB 中的所有集合名称。这里,我们有一个包含一些集合的数据库"sample"。首先,您需要切换到"sample"数据库。查询如下:
> use sample; switched to db sample
现在借助 show 命令显示所有集合名称。查询如下:
> show collections;
输出结果如下:
bookInformation userInformation
上面,您可以看到我们有以下两个集合:
bookInformation
userInformation
现在让我们删除集合名称"bookInformation"。查询如下:
> db.bookInformation.drop();
以下是显示 true 的输出:
true
结果 true 表示我们已成功删除集合。现在,您可以借助 show 命令显示所有集合名称进行验证。查询如下:
> show collections;
输出结果如下:
userInformation
查看上面的示例输出,没有名为"bookInformation"的集合。

