Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoose schema creation

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?

like image 834
Yaron Naveh Avatar asked Apr 10 '12 00:04

Yaron Naveh


People also ask

What is schema in Mongoose?

A Mongoose schema defines the structure of the document, default values, validators, etc., whereas a Mongoose model provides an interface to the database for creating, querying, updating, deleting records, etc.

How do I create a schema in MongoDB?

A schema is a JSON object that defines the the structure and contents of your data. You can use App Services's BSON schemas, which extend the JSON Schema standard, to define your application's data model and validate documents whenever they're created, changed, or deleted.

What is Mongoose schema explain with example?

When working with NodeJS, we can use mongoose ODM to define a schema for a MongoDB collection. A mongoose schema defines the shape of documents inside a particular collection. In this article, we will discuss how to create a schema in a mongoose with the help of an example.


2 Answers

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.

like image 189
freakish Avatar answered Sep 26 '22 03:09

freakish


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.

like image 20
fbeshears Avatar answered Sep 26 '22 03:09

fbeshears