Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
2 views
in SQL by (47.6k points)

Is there a set of preferred naming conventions for MongoDB entities 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

2 Answers

0 votes
by (106k points)

  • MongoDB is like JavaScript, so utilize JS naming conventions of camelCase.

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

0 votes
by (190 points)

I am mentioning the conventions and some limitation.
Collection names

  • The name should be a plural of the types of documents to be saved.
  • Use camelCase. Normally you shouldn’t have to because the collection name will be one word (plural of the types of documents saved).
  • 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 drivers available for database do not support “$” in collection name.

Database names

  • Try to have the database named after the project and one database per project.
  • Use camelCase.
  • 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.

Field names

  • Use camelCase.
  • Don’t use _ underscore as the starting character of the field name. The only field with _ underscore should be _id.
  • Field names cannot contain . dots or null characters and must not start with a $ dollar sign.

Related questions

0 votes
1 answer
0 votes
0 answers
asked Jun 24, 2021 in SQL by Harsh (1.5k points)
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Sep 6, 2019 in SQL by Sammy (47.6k points)

Browse Categories

...