Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a convention to name collection in MongoDB?

I would like to know if there is a convention for database collections such as:

PageVisit or page_visit.

Are there any advantages/disadvantages for these notations?

like image 394
johnlemon Avatar asked Mar 26 '12 07:03

johnlemon


People also ask

Are collection names case sensitive in MongoDB?

Database names and Collection names are case sensitive. You can always recreate the DB/Collection with the appropriate name. The Mongo Shell is a interactive JS interpreter.

What is the structure of MongoDB database collection document?

In MongoDB, the data records are stored as BSON documents. Here, BSON stands for binary representation of JSON documents, although BSON contains more data types as compared to JSON. The document is created using field-value pairs or key-value pairs and the value of the field can be of any BSON type.


2 Answers

The general conventions are:

  • Lowercase names: this avoids case sensitivity issues, as 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 ;)

Examples:

users pagevisits users.pagevisits 

Field name conventions (should) follow some of the same logic although camel casing those is fairly common.

like image 149
Remon van Vliet Avatar answered Sep 23 '22 02:09

Remon van Vliet


Just avoid using hyphens in your collection names.

And that's only because, if you use the cli of the two below calls, the first is invalid JavaScript:

db.foo-bar.find(); db['foo-bar'].find(); 

They are both functionally identical, but the second is slightly more annoying to type and doesn't tab-complete.

Apart from that, advantages/disadvantages depend on your use of the collections. Being consistent is more important than which convention you choose.

like image 24
AD7six Avatar answered Sep 22 '22 02:09

AD7six