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
Can we use the "." symbol in MongoDB collection name?
Yes, we can use the "." symbol in MongoDB collection names. However, you must use the getCollection() method to access collections containing dots, as the standard dot notation would be interpreted as nested properties.
Syntax
db.getCollection('collection.name.with.dots')
Example: Creating and Using Collection with Dots
Let's create a collection named "demo28.example" and insert documents ?
db.getCollection('demo28.example').insertMany([
{"Name": "Chris", "Age": 32},
{"Name": "Bob", "Age": 31},
{"Name": "David", "Age": 33}
]);
{
"acknowledged": true,
"insertedIds": [
ObjectId("5e15fbe48f2315c2efc48e6b"),
ObjectId("5e15fbec8f2315c2efc48e6c"),
ObjectId("5e15fbf38f2315c2efc48e6d")
]
}
Querying the Collection
Display all documents using the find() method ?
db.getCollection('demo28.example').find().pretty();
{
"_id": ObjectId("5e15fbe48f2315c2efc48e6b"),
"Name": "Chris",
"Age": 32
}
{
"_id": ObjectId("5e15fbec8f2315c2efc48e6c"),
"Name": "Bob",
"Age": 31
}
{
"_id": ObjectId("5e15fbf38f2315c2efc48e6d"),
"Name": "David",
"Age": 33
}
Key Points
- Dots are allowed in collection names but require
getCollection()method for access. - Standard notation like
db.demo28.examplewon't work as MongoDB interprets it as nested properties. - Always wrap the collection name in quotes when using
getCollection().
Conclusion
MongoDB allows dots in collection names, but you must use db.getCollection('name.with.dots') syntax to access them. This ensures MongoDB doesn't misinterpret the dots as object property separators.
Advertisements
