Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
How to drop a numeric collection from MongoDB?
In MongoDB, numeric collection names cannot be accessed using standard dot notation. To drop a numeric collection, use the getCollection() method followed by drop().
Syntax
db.getCollection("numericCollectionName").drop();
Create Sample Data
First, create a numeric collection ?
db.createCollection("2536464");
{ "ok" : 1 }
Now insert some documents in the collection ?
db.getCollection("2536464").insertMany([
{"Record": 1},
{"Record": 2},
{"Record": 3}
]);
{
"acknowledged" : true,
"insertedIds" : [
ObjectId("5ca254a46304881c5ce84b8e"),
ObjectId("5ca254a76304881c5ce84b8f"),
ObjectId("5ca254a96304881c5ce84b90")
]
}
Verify Collection Data
Display all documents to confirm the data ?
db.getCollection("2536464").find();
{ "_id" : ObjectId("5ca254a46304881c5ce84b8e"), "Record" : 1 }
{ "_id" : ObjectId("5ca254a76304881c5ce84b8f"), "Record" : 2 }
{ "_id" : ObjectId("5ca254a96304881c5ce84b90"), "Record" : 3 }
Drop the Numeric Collection
Remove the numeric collection using the drop method ?
db.getCollection("2536464").drop();
true
Key Points
-
getCollection()is required because numeric names cannot use dot notation. - The
drop()method returnstrueif the collection is successfully deleted. - All documents and indexes in the collection are permanently removed.
Conclusion
Use db.getCollection("numericName").drop() to remove collections with numeric names. The getCollection() method handles special characters that standard dot notation cannot process.
Advertisements
