Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Capitalization of the model name (Mongoose)?

Why the name of the Model is Capitalized. As in their documentation, they have capitalized it.

var schema = new mongoose.Schema({ name: 'string', size: 'string' });
var Tank = mongoose.model('Tank', schema);

Why is Tank capitalized here? Is there any specific reason?

Sorry if this is not a good question. Any help would be appreciated :)

like image 356
Sanyam Avatar asked Dec 06 '25 18:12

Sanyam


2 Answers

This is merely a coding convention. The Tank model is being viewed as an instantiable class:

var small = new Tank({ size: 'small' });

According to typical coding conventions, class names should be UpperCamelCase with the first letter capitalised, and instance variables should be in lowerCamelCase (as should methods).

like image 161
cmbuckley Avatar answered Dec 09 '25 13:12

cmbuckley


Reference Mongoose Documentation

var schema = new mongoose.Schema({ name: 'string', size: 'string' });
var Tank = mongoose.model('Tank', schema);

The first argument is the singular name of the collection your model is for. ** Mongoose automatically looks for the plural, lowercased version of your model name. ** Thus, for the example above, the model Tank is for the tanks collection in the database.

like image 35
Nikhil Vats Avatar answered Dec 09 '25 11:12

Nikhil Vats