How to drop a numeric collection from MongoDB?

MongoDBDatabaseBig Data Analytics

In order to remove the numeric collection name, use the following syntax

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

First, create a numeric collection. Following is the query

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

Now insert some documents in the above collection. Following is the query

> 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")
}

Following is the query to display all documents from a collection with the help of find() method

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

This will produce the following output

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

Following is the query to remove numeric collection name

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

This will produce the following output

True

Now the numeric collection name has been removed successfully.

raja
Published on 15-Apr-2019 07:53:14
Advertisements