Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use more than one schema per collection on mongodb

I wanted to use more than one schema per collection in mongodb , how to use it....?
It gives me this error when I try to run it:

Error:

OverwriteModelError: Cannot overwrite allUsers model once compiled.
OverwriteModelError: Cannot overwrite checkInOut model once compiled.


Heres my schema.js

   var mongoose = require('mongoose');        var Schema = mongoose.Schema           , ObjectId = Schema.ObjectId;     var checkInInfoSchema= new Schema({        name:String,        loginSerialId:Number    });      var loginUserSchema = new Schema({           sn : { type: Number, unique:true }           ,uname: {type:String, unique:true}           ,pass:String       });     var registerUserSchema = new Schema({        sn : { type: Number, unique:true }        , name: String   //his/her name        ,pass:String,        companyKey:{type:String},        uname:{type:String,unique:true}    });       var checkInOutSchema = new Schema({        uname: String        ,companyKey:String        ,task:String        ,inTime:String        ,outTime:String        ,date:{type:String}        ,serialId:{type:Number,unique:true}        ,online:Boolean    });     //Different schema for same collection "allUsers"            var allUser=mongoose.model('allUsers',loginUserSchema);            var registerUser=mongoose.model('allUsers',registerUserSchema);      //Different schema for same collection "checkInOut"    var checkInOut=mongoose.model('checkInOut',checkInOutSchema);    var checkInInfo=mongoose.model('checkInOut',checkInInfoSchema);     module.exports={         allUser:allUser,         registerUser:registerUser,         checkInOut:checkInOut,        checkInInfo:checkInInfo    }; 
like image 915
TaLha Khan Avatar asked Jan 22 '13 07:01

TaLha Khan


People also ask

Can we have multiple schemas in MongoDB?

Although, MongoDB supports schema validation, but it will validate all the documents in a collection against the same schema. It means, that you can not have multiple schemas for a single collection.

Can MongoDB have schema?

Data in MongoDB has a flexible schema. Collections do not enforce document structure by default. This flexibility gives you data-modeling choices to match your application and its performance requirements.

How do I save a schema in MongoDB?

createModel = function(model, callback) { var model_to_create = new Model(model); console. log("creating a schema..." + model) model_to_create. save(function(err,data){ callback(err, data); // <-- called when save is ready }); }; ... var schema = require('../schemas/schema'); schema.

How do I join two collections in MongoDB using node JS?

Join Collections MongoDB is not a relational database, but you can perform a left outer join by using the $lookup stage. The $lookup stage lets you specify which collection you want to join with the current collection, and which fields that should match.


1 Answers

In mongoose you can do something like this:

var users = mongoose.model('User', loginUserSchema, 'users'); var registerUser = mongoose.model('Registered', registerUserSchema, 'users'); 

This two schemas will save on the 'users' collection.

For more information you can refer to the documentation: http://mongoosejs.com/docs/api.html#index_Mongoose-model or you can see the following gist it might help.

like image 111
Julián Duque Avatar answered Oct 17 '22 04:10

Julián Duque