Follow by Email

Schema design and Naming conventions in MongoDB


Naming convention for collection

In order to name a collection few precautions to be taken
  • A collection with empty string (“”) is not a valid collection name.
  • A collection name should not contain the null character because this defines the end of collection name.
  • Collection name should not start with the prefix “system.” as this is reserved for internal collections.
  • It would be good to not contain the character “$” in the collection name as various driver available for database do not support “$” in collection name.
Databases in MongoDB
So we a document. A group of documents is termed as collection. And a group of collections is a database. Every database has its own permission and is stored in a separate file on disk.
Tip* : It would be good to store all the data related to a single application in one single database.


Property Name Format:


Property names must follow below guidelines:
  • Property names should have meaningful names with defined semantics.
  • Property names must be camel-cased, ASCII strings.
  • The first character must be a letter, an underscore (_) or a dollar sign ($).
  • Subsequent characters can be a letter, a digit, an underscore, or a dollar sign.
  • Reserved JavaScript keywords should be avoided (A list of reserved JavaScript keywords can be found below).

These guidelines mirror the guidelines for naming JavaScript identifiers. This allows JavaScript clients to access properties using dot notation.

Things to keep in mind while creating a database name is
  • A database with empty string (“”) is not a valid database name.
  • Database name cannot be more than 64 bytes.
  • Database name are case-sensitive, even on non-case-sensitive file systems. Thus it is good to keep name in lower case.
  • A database name cannot contain any of these characters “/, \, ., “, *, <, >, :, |, ?, $,”. It also cannot contain a single space or null character.
There are several special databases:
  • admin – This is “root” database in terms of authentication. There are several server-side commands that can only be run from admin database. Such as listing all the databases.
  • local – This database is never replicated and can be used to store any collections that should be local to a single server.
  • config – This database is used at the time of sharding, to store information about the shards.
  • By concatenating a database name with a collection in that database you can get a fully qualified collection name called a namespace.
For example: an employee collection inside a database named mydb can be – mydb.employee
Namespaces are limited to 121 bytes in length and, in practice, should be fewer than 100 bytes long.
Lowercase names : avoids case sensitivity issues, MongoDB collection names are case sensitive.
 Plural : more obvious to label a collection of something as the plural, e.g. "files" rather than "file"
>No word separators : Avoids issues where different people (incorrectly) separate words (username <-> user_name, first_name <-> firstname). This one is up for debate according to a few people around here but provided the argument is isolated to collection names I don't think it should be ;) If you find yourself improving the readability of your collection name by adding underscores or camelCasing your collection name is probably too long or should use periods as appropriate which is the standard for collection categorization.
Dot notation for higher detail collections : Gives some indication to how collections are related. For example you can be reasonably sure you could delete "users.pagevisits" if you deleted "users", provided the people that designed the schema did a good job.