Talk:Defining a Model

What is a "Model"?
A model at its most basic form is a system for storing and retrieving information. Its one of the holy trinity of MVC (Model View Collection). Traditionally Model was tacitly assumed to be a database; now datatabases are one of many types of model.

In the context of a programming language model is often shorthand for a "Model Proxy" - a set of libraries for acessing a model within the context of a language. That is, the part of an ORM (Object Relational Mapper) that represents a single distinct collection.

In the context of Mongoose, a model can represent both a single document and the collection in which those documents are stored. This can be a little confusing - but it is part of the Domain Pattern, that is, a model that incorporates behavior and data.

The useful takeaway is that the same object that you use to query a collection can be used to function as an ActiveRecord. var users = mongoose.model('Users'); // assuming that has already been bound to a schema users.findOne({name: "bob"}, function(err, bob) {    console.log("My Name is Bob and I'm a Mongoholic", bob); }); var billy = new mongoose.model("Users"); // note the NEW keyword billy.name = "Billy"; billy.save; That being said - while this specific bit of knowledge is true in the world of Mongoose its not the only incarnation of models. In fact in a persistent node application you can ad-hoc a model with a persistent array // an ad-hoc model in the form of a simple node module module.exports = { users: [{name:"Billy"}, {name: "Bob"}] } and while this model will not outlast the process withn which it runs, in some contexts this simple model is actaully quite effective - for instance, when you have a configuration read-only file, an ad-hoc model is quite adequate.