Intellipaat 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

3 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.
0 votes
ago by (1.1k points)

It is reasonable and consistent to recommend the naming conventions for entities in MongoDB as proposed in this paper. Below provides an extensive explanation on the naming conventions for a mongodb database, its collections and document fields:

1. Databases  

  • Format: all in lower case letters with 'db' as the last word.  

  • Style: to be used in singular form.  

  • Examples of use: imagedb, resumedb, memberdb.

2. Collections  

  • Format: all in lower case.  

  • Style: since all the collections are under men’s wear, plural nouns will be used, for they possess different documents of the same kind.  

  • Examples: images, resumes, members.

3. Document Fields  

  • Format: lowerCamelCase.  

  • Style: use words that are appropriate in describing the contents of the field.  

  • Examples: memberFirstName, fileName, createdAt, updatedAt.

Additional Consideration  

  • Use of Underscores: Some applications may use Snake case or any other forms, but in MongoDB JavaScript, the most popular language, lower Camel Case is predominant.  

  • Avoid Reserved Words: Names should not contain MongoDB reserved words and special characters since that may cause difficulties.  

  • Consistency: Non-compliance of the conventions will result in a codebase that is difficult to read, maintain and scale due to irregular naming on databases, collections and fields.  

  • Length: Give a descriptive name adequate in length. Do not give names that are excessively long except where proper names may need to be used.

Conclusion.  

The policies that you have developed in regard to naming the encyclopedic contents of mongo database systems are straightforward and well each expressed. The database administration system will certainly benefit from better modeling by applying more layer packing concepts.

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)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...