从 MongoDB 中删除包含特殊字符的集合?
mongodbdatabasebig data analytics更新于 2026/1/27 13:37:17
要删除包含 _ 或 - 等特殊字符的集合,您需要使用以下语法 −
db.getCollection("yourCollectionName").drop();
为了理解这个概念,让我们创建一个包含文档的集合。创建包含文档的集合的查询如下 −
> db.createCollection("_personalInformation");
{ "ok" : 1 }
> db.getCollection('_personalInformation').insertOne({"ClientName":"Chris","ClientCountryName":"US"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9158bb4afe5c1d2279d6b2")
}
> db.getCollection('_personalInformation').insertOne({"ClientName":"Mike","ClientCountryName":"UK"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9158c84afe5c1d2279d6b3")
}
> db.getCollection('_personalInformation').insertOne({"ClientName":"David","ClientCountryName":"AUS"});
{
"acknowledged" : true,
"insertedId" : ObjectId("5c9158d54afe5c1d2279d6b4")
}
使用 find() 方法显示集合中的所有文档。查询如下 −
> db.getCollection('_personalInformation').find().pretty();
以下是输出 −
{
"_id" : ObjectId("5c9158bb4afe5c1d2279d6b2"),
"ClientName" : "Chris",
"ClientCountryName" : "US"
}
{
"_id" : ObjectId("5c9158c84afe5c1d2279d6b3"),
"ClientName" : "Mike",
"ClientCountryName" : "UK"
}
{
"_id" : ObjectId("5c9158d54afe5c1d2279d6b4"),
"ClientName" : "David",
"ClientCountryName" : "AUS"
}
以下查询用于从 MongoDB 中删除包含特殊字符的集合 −
> db.getCollection("_personalInformation").drop();
以下是输出 −
True
结果 TRUE 表示我们已从 MongoDB 中完全删除该集合。

