103

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
johnlemon
  • 20,761
  • 42
  • 119
  • 178
  • 2
    See also http://stackoverflow.com/questions/5916080/what-are-naming-conventions-for-mongodb – Gary Dec 06 '12 at 11:54

5 Answers5

109

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.

silkfire
  • 24,585
  • 15
  • 82
  • 105
Remon van Vliet
  • 18,365
  • 3
  • 52
  • 57
  • 43
    I'm going to have to disagree with you for the whole "no word separators" thing, especially if you are using MongoDB with Python. Underscores instead of spaces is highly readable, which is one of the most desirable qualities of code, schema and the like. – Noah McIlraith Dec 27 '12 at 11:15
  • 2
    Subjectivity is a beautiful thing ;) If you find yourself with field or collection names with so many words in them that underscores improve code readability you should probably reconsider the name as a whole really. To each their own of course. I find the issue of possible inconsistencies in what words get seperated a much bigger issue. Not using word seperators is relatively language agnostic. Where you prefer underscores a Java developer would probably prefer camelcasing and that becomes messy in a hurry. – Remon van Vliet Jan 09 '13 at 15:31
  • 8
    Singular rather than plural otherwise it maps to Objects like Pojos in Plural and you end up with a Users class! rather than a User class – ricardoespsanto Apr 23 '14 at 09:40
  • 1
    @Ricky It's the opposite. "users" contains zero or more seperate users. A POJO maps to a single of those users. In other words, you need 2 POJO instances to represent the two users in the "users" collection. Collections are literally that, collections. You wouldn't write Java code like "List user;" – Remon van Vliet Apr 23 '14 at 14:09
  • 1
    @RemonvanVliet I would write List listOfUsers; or List users; I would prefer the collection, the pojo and everything to be singular. – ricardoespsanto Apr 30 '14 at 16:04
  • 1
    @Ricky That sounds contradictory to me. For the exact reason you prefer List users is why you should name the collection "users" as the collection in MongoDB is the collection in Java. What you're suggesting is the equivalent of writing List user; in Java – Remon van Vliet Jun 02 '14 at 15:25
  • @RemonvanVliet I guess it gets down to personal preference but see (http://stackoverflow.com/questions/338156/table-naming-dilemma-singular-vs-plural-names) and check what's the most voted answer. I learned in Uni to keep it singular and it still makes sense even though I can understand your position on List of user ... – ricardoespsanto Jun 02 '14 at 16:50
  • 4
    @Ricky It is I suppose. The answer you're referring to is mixing up the same concepts that are mixed up here though. Naming the entity versus naming the collection. Nobody would use "Customers" as the entity name as the author of that answer suggests even though they might use CUSTOMERS as the SQL table name. As you say it's an extremely subjective discussion. As long as it's consistent across an entire codebase there shouldn't be too much of an issue. – Remon van Vliet Jun 05 '14 at 10:50
  • 4
    I think singular makes more sense, because the plural is implied. And as @Ricky says, this standard was established in sql long ago. [stackoverflow.com/questions/338156](http://stackoverflow.com/questions/338156/table-naming-dilemma-singular-vs-plural-names) – steampowered Jun 11 '15 at 23:45
  • 2
    Using "dot notation for higher detail collections" seems to differ from how MongoDB itself names collections. This may be a special case but GridFS uses dot notation to indicate related collections, not higher detail collections. For example, in GridFS you'll have collections `myfiles.files` and `myfiles.chunks`, not `myfiles` and `myfiles.chunks`. – Paul Dec 08 '15 at 17:57
  • after many years of software dev, singular instead of plural is better, one less character :) rabbit not rabbits :) –  Sep 10 '19 at 19:58
  • 1
    Just a small correction to the answer above: database name are case insensitive (not "sensitive"). Also, mongo will plural your collection name if not specified. "course" will become "courses". "Since database names are case insensitive in MongoDB, database names cannot differ only by the case of the characters. " (from Mongo doc) Because of them, try to name all your collection in lowercase and without special characters. You'll avoid a lot of error - especially if you use Mongoose. Mongoose has some weird querying specificities. See my answer below the page. – Dom355 Jan 07 '22 at 00:07
  • This answer is opinion. https://www.mongodb.com/docs/manual/faq/fundamentals/#how-do-i-create-a-database-and-a-collection- In this instance, the documentation uses camel case, singular. – dQw4w9WyXcQ Oct 23 '22 at 22:40
38

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
AD7six
  • 63,116
  • 12
  • 91
  • 123
  • 1
    Are `:` valid for namespacing collection names, as in `foo:bar`? – raffian Mar 26 '12 at 18:58
  • 1
    anything is valid for mongo. e.g. `db["\n"].insert({});` - no error. The things to consider are mostly convenience with the driver you are using. – AD7six Mar 26 '12 at 19:56
  • 1
    This changed in 2.2 on windows: /\. "*<>:|? are no longer valid - see http://docs.mongodb.org/manual/release-notes/2.2/#rn-2-2-database-name-restriction-windows – toong Mar 05 '13 at 12:38
  • 5
    @toong, that's for _database_ names, not collections. – BorisOkunskiy Jun 26 '13 at 10:10
20

In http://docs.mongodb.org/manual/reference/limits/, the manual states that the collection names should begin with an underscore ('_') or a letter character, and cannot:

  • contain the $.
  • be an empty string (e.g. "").
  • contain the null character.
  • begin with the system. prefix. (Reserved for internal use.)

However, if you follow the rule and create a collection with '_' as the beginning letter, such as "_TWII", you will encounter trouble when you want to drop the collection. See the test below and the way to fix it. Collection '_TWII' was created under 'people' db.

> show collections
_TWII
employees
system.indexes
> db._TWII.drop()
2015-02-19T16:34:56.738-0800 TypeError: Cannot call method 'drop' of undefined

> use admin
switched to db admin

> db.runCommand({renameCollection:"people._TWII",to:"people.TWII"})
{ "ok" : 1 }
> use people
switched to db people
> show collections
TWII
employees
system.indexes
> db.TWII.drop()
true
> show collections
employees
system.indexes
> 

A short-cut to delete _TWII collection while under 'people' db:

> db.createCollection('^TWII')
{ "ok" : 1 }
> db.getCollection('^TWII').drop()
true
Scribblemacher
  • 1,518
  • 1
  • 16
  • 31
Daniel C. Deng
  • 1,573
  • 13
  • 8
5

MongoDB has some naming conventions. One of them is that database name are case insensitive. Also, mongo will plural your collection name if not specified. "course" will become "courses".

Since database names are case insensitive in MongoDB, database names cannot differ only by the case of the characters.

Because of them, try to name all your collection in lowercase and without special characters. You'll avoid a lot of error - especially if you use Mongoose. Mongoose has some weird querying specificities.

For example, if you have a collection named "courses", here's how you need to structure your model:

const LawModel = mongoose.model(
  "course",
  new mongoose.Schema({
    id: String,
    name: String,

  }),

Note how "course" is singular? Mongoose will plural it hence why you might see an empty array "[]". --> you are querying an unexisting collection.

Try renaming and adjusting your model.

Dom355
  • 171
  • 2
  • 15
  • 2
    The pluralization thing really fascinated me. They were pretty painstaking with that. I got curious where my collection name came from since I didn't remember specifying it. I had a document named glossary and Mongo was aware enough to change the y to i and add es, instead of just plopping on an s! – Chris Walker Apr 24 '21 at 19:55
  • I know! had to dig into the doc to solve the same issue. The top answer in this question mentions that MongoDB's conventions are case "sensitive" when they are in fact "insensitive". Hence why I copied the above excerpt from the documentation. Cheers! – Dom355 Apr 27 '21 at 23:58
  • @ChrisWalker guess the result when I created a collection name `human`. Hint, mongoose didn't go through the popping an s route...lol – BrunoElo Jun 13 '22 at 14:02
0

The docs use camelCase.

https://www.mongodb.com/docs/manual/faq/fundamentals/#how-do-i-create-a-database-and-a-collection-

It's also worth noting that Mongoose will infer the collection name by pluralizing the model name if no collection name is specified.

https://mongoosejs.com/docs/guide.html#collection

I suggest plural camel case.

dQw4w9WyXcQ
  • 125
  • 1
  • 8