280

Is there a set of preferred naming conventions for MongoDB entitites such as databases, collections, field names?

I was thinking along these lines:

  • Databases: consist of the purpose (word in singular) and end with “db” – all lower case: imagedb, resumedb, memberdb, etc.
  • Collections: plural in lower case: images, resumes,
  • Document fields: lowerCamelCase, e.g. memberFirstName, fileName, etc
Andrey
  • 20,487
  • 26
  • 108
  • 176

6 Answers6

184
  1. Keep'em short: Optimizing Storage of Small Objects, SERVER-863. Silly but true.

  2. I guess pretty much the same rules that apply to relation databases should apply here. And after so many decades there is still no agreement whether RDBMS tables should be named singular or plural...

  3. MongoDB speaks JavaScript, so utilize JS naming conventions of camelCase.

  4. MongoDB official documentation mentions you may use underscores, also built-in identifier is named _id (but this may be be to indicate that _id is intended to be private, internal, never displayed or edited.

Beau Smith
  • 33,433
  • 13
  • 94
  • 101
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • 117
    3 and 4 are kind of contradictory -- JS prefers camelcase, Mongo seems to prefer underscores... but when in doubt, go for underscores. People accustomed to non-latin alphabets will thank you. – Matt Zukowski Sep 26 '11 at 20:33
  • 2
    See this question for the single vs plural debate: http://stackoverflow.com/questions/338156/table-naming-dilemma-singular-vs-plural-names – Jason Apr 21 '16 at 07:31
  • 6
    I'm not sure I'd say "JS prefers camelcase". JS itself has no preference, but it's perhaps true to say that *most* JS programmers tend to use camel case. – treeface Nov 16 '16 at 02:00
  • 7
    @treeface I think Matt was referring to the fact JS's built-in methods all use camelCase, both in node and in browsers – Luke Taylor Jan 12 '18 at 00:45
  • 4
    The built-in identifier `_id` is most-likely prefixed with an underscore to follow a common JavaScript convention which indicates that the key is meant to be an internal/private key. In other words, the `_id` is not intended to ever be edited or presented to anyone viewing the data of a collection. – Beau Smith Mar 02 '18 at 22:54
  • Does the language of the application that interacts with MongoDB have any bearing on the naming convention? E.g., in a Python application it's recommended to use snake_case, but in a Node.js application it's recommended to use camelCase? – KeepingItClassy Apr 19 '18 at 06:56
  • I would like to specifically discourage using hyphens in database names: `use mydb-staging` does not work! – Casper Gerritsen Oct 22 '18 at 13:26
  • i'd say stick with lower case just to please linux – DragonFire May 15 '20 at 00:34
  • 2
    With the arrival of WiredTiger and compression, it's no longer true that fields should have short names. – Andreas Bergström Sep 10 '20 at 08:52
  • 1 and 4 also contradict. myField is shorter than my_field – Page not found May 11 '22 at 11:04
  • lowerCamelCase, CamelCase, snake_case. – Liran Feb 28 '23 at 09:53
134

DATABASE

  • camelCase
  • append DB on the end of name
  • make singular (collections are plural)

MongoDB states a nice example:

To select a database to use, in the mongo shell, issue the use <db> statement, as in the following example:

use myDB
use myNewDB

Content from: https://docs.mongodb.com/manual/core/databases-and-collections/#databases

COLLECTIONS

  • 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.

Content from: https://web.archive.org/web/20190313012313/http://www.tutespace.com/2016/03/schema-design-and-naming-conventions-in.html

For collections I'm following these suggested patterns until I find official MongoDB documentation.

KwodKewe
  • 63
  • 1
  • 9
BBi7
  • 1,588
  • 1
  • 11
  • 8
  • _"Lowercase names: avoids case sensitivity issues"_ only if everyone knows to use lower case. Even then the engine should be able to recognize that there is (and should be) no logical difference between "CollectionA" and "collectionA"2 – StingyJack Apr 04 '21 at 19:51
  • It's worth noting that there doesn't seem to be an official convention for collection names. ([Source](https://www.mongodb.com/docs/manual/reference/limits/#naming-restrictions)) Though it does appear the community has established its own conventions, for example the Mongoose library unless specified otherwise, chooses a default collection name which is the model name, to lowercase, then pluralized. i.e. `Video` → `videos` ([Source](https://mongoosejs.com/docs/api/mongoose.html#mongoose_Mongoose-model)) – Seth Falco May 21 '22 at 16:59
  • document fields also lowerCamelCase, example https://www.mongodb.com/docs/manual/core/timeseries/timeseries-procedures/ – Ilya Petukhov Jun 11 '23 at 14:02
33

Even if no convention is specified about this, manual references are consistently named after the referenced collection in the Mongo documentation, for one-to-one relations. The name always follows the structure <document>_id.

For example, in a dogs collection, a document would have manual references to external documents named like this:

{
  name: 'fido',
  owner_id: '5358e4249611f4a65e3068ab',
  race_id: '5358ee549611f4a65e3068ac',
  colour: 'yellow'
  ...
}

This follows the Mongo convention of naming _id the identifier for every document.

Yves M.
  • 29,855
  • 23
  • 108
  • 144
danza
  • 11,511
  • 8
  • 40
  • 47
13

Naming convention for collection

In order to name a collection few precautions to be taken :

  1. A collection with empty string (“”) is not a valid collection name.
  2. A collection name should not contain the null character because this defines the end of collection name.
  3. Collection name should not start with the prefix “system.” as this is reserved for internal collections.
  4. 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.

Things to keep in mind while creating a database name are :

  1. A database with empty string (“”) is not a valid database name.
  2. Database name cannot be more than 64 bytes.
  3. Database name are case-sensitive, even on non-case-sensitive file systems. Thus it is good to keep name in lower case.
  4. A database name cannot contain any of these characters “/, , ., “, *, <, >, :, |, ?, $,”. It also cannot contain a single space or null character.

For more information. Please check the below link : http://www.tutorial-points.com/2016/03/schema-design-and-naming-conventions-in.html

7

I think it's all personal preference. My preferences come from using NHibernate, in .NET, with SQL Server, so they probably differ from what others use.

  • Databases: The application that's being used.. ex: Stackoverflow
  • Collections: Singular in name, what it's going to be a collection of, ex: Question
  • Document fields, ex: MemberFirstName

Honestly, it doesn't matter too much, as long as it's consistent for the project. Just get to work and don't sweat the details :P

Rex Morgan
  • 2,979
  • 2
  • 21
  • 32
  • 1
    I think the one that could have consequences is the document fields, since they'll be stored inside each document. As Tomasz pointed out, keeping them short should save space/bandwidth. I think it's much more important that you use something that makes it easy to understand, though. – Rex Morgan May 06 '11 at 19:44
3

Until we get SERVER-863 keeping the field names as short as possible is advisable especially where you have a lot of records.

Depending on your use case, field names can have a huge impact on storage. Can't understand why this is not a higher priority for MongoDb, as this will have a positive impact on all users. If nothing else, we can start being more descriptive with our field names, without thinking twice about bandwidth & storage costs.

Please do vote.

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56
FlappySocks
  • 3,772
  • 3
  • 32
  • 33
  • 3
    Just to update potential future readers of this answer, MongoDB does compression these days, so long field names are not really an issue anymore. – Hubro Feb 26 '20 at 20:26