如何从 MongoDB 中删除数字集合?


若要删除数字集合名称,请使用以下语法

db.getCollection("yourNumericCollectionName").drop();

首先,创建一个数字集合。以下为查询

> db.createCollection("2536464");
{ "ok" : 1 }

现在在上述集合中插入一些文档。以下为查询

> db.getCollection("2536464").insertOne({"Record":1});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ca254a46304881c5ce84b8e")
}
> db.getCollection("2536464").insertOne({"Record":2});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ca254a76304881c5ce84b8f")
}
> db.getCollection("2536464").insertOne({"Record":3});
{
   "acknowledged" : true,
   "insertedId" : ObjectId("5ca254a96304881c5ce84b90")
}

以下为使用 find() 方法显示集合中所有文档的查询

> db.getCollection("2536464").find().pretty();

这将生成以下输出

{ "_id" : ObjectId("5ca254a46304881c5ce84b8e"), "Record" : 1 }
{ "_id" : ObjectId("5ca254a76304881c5ce84b8f"), "Record" : 2 }
{ "_id" : ObjectId("5ca254a96304881c5ce84b90"), "Record" : 3 }

以下为删除数字集合名称的查询

> db.getCollection("2536464").drop();

这将生成以下输出

True

现在数字集合名称已成功删除。

更新于:30-Jul-2019

98 次查看

启动您的 职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.