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?
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?
The general conventions are:
Examples:
users
pagevisits
users.pagevisits
Field name conventions (should) follow some of the same logic although camel casing those is fairly common.
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.
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
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.
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.