Why does the model name matter in Mongoose? I understand that given a model name, Mongoose can try to guess the collection name. However, for this application it seems better and easier to just explicitly specify the collection name instead of the model name.
What else is the model name used for in Mongoose?
Mongoose is an Object Data Mapping (ODM) library for node similar to Object Relational Mapping (ORM) in Ruby on Rails and some other frameworks. Therefore, it follows more or less the same rules (or guidelines, as you can override them):
ORM:
Class name capital and singular
Table name small and plural
Class object mapped/translated to table row and vice versa
Class and its objects have methods/hooks
ODM:
Model name capital and singular (e.g. User)
Collection name small and plural (e.g. users)
Model object mapped/translated to collection document and vice versa (e.g. object of model: new User({name: "abc"}), document in collection: {"name": "abc"})
Model and its objects have methods/hooks (e.g. pre, post hooks; validations etc)
The mapping is between incompatible types, like an object of Ruby class to a row of SQL table; and a JavaScript object of Mongoose model (can have functions, for instance) to a JSON document of mongodb (valid JSON can't have functions).
Since class and table, model and collection are different entities (though related) and serve different purpose, they are named differently (though similarly). Hence the need of model name in Mongoose!
Related
I have defined db name in squelize constructor so that tables being generated by corresponding model files. Tables are successfully created in given db. I am confused with the schema which by default is public in models.So why to define schema in models if tables are properly generated in db in default postgresql server named PostgreSQL 15. if I change name of schema from public to some how ALGH it says schema doesnot exist. if it is referencing schema then what is the purpose of giving db name in sequelize constructor.
You can have multiple schemas in the same DB for different purposes like to divide sets of tables for different apps/services or by functionality.
You can indicate a schema in the Sequelize instance and it will be the default schema for all models that does not have an explicit schema OR you can indicate a schema for certain models only and other models will have the default public schema.
Of course, you need to create tables only after you decided and indicated all schemas (either in Sequelize instance and/or in certain models) you need otherwise you will get such errors you already mentioned in the post.
I was learning Node.js and Mongoose. Then, I cam across the term schema which is defined as the description of the structure of the data, default values and validation. So, as we know schema looks basically like this:
var TaskSchema = new Schema({
name: String,
priority: Number
});
The question I would like to ask is Why should we care about the description of structure of data that the schema allows to achieve, I mean, what is the main point in the use of schemas in mongoose? if you say, validation, then can't we achieve that with express-validator package? or...
Everything in Mongoose starts with a Schema. Each schema maps to a MongoDB collection and defines the shape of the documents within that collection.
See the documentation for more information.
Schemas not only define the structure of your document and casting of properties, they also define document instance methods, static Model methods, compound indexes, and document lifecycle hooks called middleware.
This is what allows you to communicate with the database easily using mongoose. It's not just for validation.
using this line :
var collection = mongoose.connection.db.collection("users");
I get users s collection, now i need to get the schema from that collection ?
Is there a way to do this? thank you.
PS: I dont have any models
if you do not have any models, I'm afraid this can't be done.
the schema is the structural representation of the document, not the collection. You can use the schema (via the model) to enforce that any document added to the collection match some structure, but there's nothing that prevents you from inserting into this collection a different document without this model, that will not match the criteria
so the schema does not live in the DB, it is a part of the mongoose module, and lives in the code. and the way to access it is through the model only
I have two has_one models (from_airport and to_airport) which gets its values from the model 'airport'.
How do i embed the ids of from_airport and to_airport and include the airport objects
using active model serializers and also include the airport object using active model serializer.
I found this (I think?) undesirable behavior in mongoose 3.8.12, when you create a new mongoose model by default it makes its name plural adding an 's' which is perfectly normal, when the model's name already ends with an 's' then save it as it is (expected behavior). The problem is when you have two different models with the same name but one plural and the other singular, then mongoose allows you two create the two but uses the same collection in mongo to store both.
let's say I have a collection named 'car':
mongoose.model('Car', schema);
and a collection 'cars':
mongoose.model('Cars', schema);
both are saved in mongo as 'cars'
I don't think that is the expected behavior.
http://mongoosejs.com/docs/api.html#index_Mongoose-model
When no collection argument is passed, Mongoose produces a collection name by passing the model name to the utils.toCollectionName method. This method pluralizes the name. If you don't like this behavior, either pass a collection name or set your schemas collection name option.
Since Car turns into Cars, and Cars is already plural you end up with the same collection name. Models are generally given the singular name. The link above lists several methods to set a custom collection name.
Of course, you can always create a new issue on the project's GitHub repo.