Field Name Rules
MongoDB has a few rules about document field names, which are listed as follows:
- The field name cannot contain a null character.
- Only the fields in an array or an embedded document can have a name starting with the dollar sign (
$
). For the top-level fields, the name cannot start with a dollar ($
) sign. - Documents with duplicate field names are not supported. According to the MongoDB documentation, when a document with duplicate field names is inserted, no error will be thrown, but the document won't be inserted. Even the drivers will drop the documents silently. On the mongo shell, however, if such a document is inserted, it gets inserted correctly. However, the resulting document will have only the second field. That means the second occurrence of the field overwrites the value of the first.
Note
MongoDB (as of version 4.2.8) does not recommend field names starting with a dollar (
$
) sign or a dot (.
). The MongoDB query language may not work correctly...