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
Which characters are NOT allowed in MongoDB field names?
MongoDB field names have specific restrictions. You cannot use the $ symbol at the beginning of field names or the period (.) character anywhere in field names, as these are reserved for MongoDB's internal operations and dot notation.
Syntax
// Invalid field names
{ "$fieldName": "value" } // Cannot start with $
{ "field.name": "value" } // Cannot contain dots
{ "field$name": "value" } // $ allowed in middle/end
// Valid field names
{ "fieldName": "value" }
{ "field_name": "value" }
{ "Employee Name": "value" } // Spaces allowed
Prohibited Characters
| Character | Restriction | Reason |
|---|---|---|
$ |
Cannot start field names | Reserved for operators ($set, $push, etc.) |
. |
Not allowed anywhere | Used for dot notation queries |
\0 |
Not allowed anywhere | Null character conflicts with BSON |
Example: Valid Field Names
db.charactersAllowedDemo.insertOne({
"Employee Name": "John",
"employee_id": 123,
"salary$amount": 50000,
"field123": "value"
});
{
"acknowledged": true,
"insertedId": ObjectId("5c7fefbc8d10a061296a3c6d")
}
Verify Result
db.charactersAllowedDemo.find().pretty();
{
"_id": ObjectId("5c7fefbc8d10a061296a3c6d"),
"Employee Name": "John",
"employee_id": 123,
"salary$amount": 50000,
"field123": "value"
}
Key Points
-
$is allowed in the middle or end of field names, just not at the beginning. - Spaces and underscores are perfectly valid in field names.
- Numbers and letters are always allowed anywhere in field names.
Conclusion
Avoid starting field names with $ and never use dots anywhere in field names. These restrictions ensure compatibility with MongoDB's query syntax and internal operations.
Advertisements
