mongoose schema creation


Question

I've just started with mongoose. I have a creation script with mongoose that creates the schemas and db with sample data.

Now I write the actual application. Do I need to create the schema object each time my application runs, or is it already available somehow?

In other words do I need to run this code in every app that uses mongoose to access the db or just the first time:

var Comments = new Schema({
    title     : String
  , body      : String
  , date      : Date
});

How would the answer change if I have setters/validations/etc?

1
42
4/10/2012 12:35:38 AM

Accepted Answer

One defines Schema so application understands how to map data from the MongoDB into JavaScript objects. Schema is a part of application. It has nothing to do with database. It only maps database into JavaScript objects. So yes - if you want to have nice mapping you need to run this code in every application that needs it. It also applies to getters/setters/validations/etc.

Note however that doing this:

var mongoose = require('mongoose');
var Schema = mongoose.Schema; // <-- EDIT: missing in the original post
var Comments = new Schema({
    title     : String
  , body      : String
  , date      : Date
});
mongoose.model("Comments", Comments);

will register Schema globaly. This means that if the application you are running is using some exterior module, then in this module you can simply use

var mongoose = require('mongoose');
var Comments = mongoose.model("Comments");
Comments.find(function(err, comments) {
    // some code here
});

(note that you actually need to register the Schema before using this code, otherwise an exception will be thrown).

However all of this works only inside one node session, so if you are running another node app which needs the access to the Schema, then you need to call the registration code. So it is a good idea to define all Schemas in separate files, for example comments.js may look like this

var mongoose = require('mongoose');
var Schema = mongoose.Schema; // <-- EDIT: missing in the original post

module.exports = function() {
    var Comments = new Schema({
        title     : String
      , body      : String
      , date      : Date
    });
    mongoose.model("Comments", Comments);
};

then create file models.js which may look like this

var models = ['comments.js', 'someothermodel.js', ...];

exports.initialize = function() {
    var l = models.length;
    for (var i = 0; i < l; i++) {
        require(models[i])();
    }
};

Now calling require('models.js').initialize(); will initialize all of your Schemas for a given node session.

76
4/19/2012 1:20:25 PM

You do need to run this initialization code every time you run your app to register your app's Schemas with mongoose.

When your app ends, mongoose does not store your Schema(s). So, the next time you run an app that uses a Schema, you need to register your Schema(s) again.

However, it's fairly easy to set up your app to do so.

Here are two links to code that demonstrates how one can initialize schemas in mongoose. The first is in JavaScript, the second is in CoffeeScript.

https://github.com/fbeshears/register_models

https://github.com/fbeshears/register_coffee_models

The JavaScript demos is just one app.

The CoffeeScript code has two separate apps. The first stores documents with MongoDB, the second finds and displays the documents stored by the first app.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon