Use more than one schema per collection on mongodb

﹥>﹥吖頭↗ 提交于 2019-12-28 05:17:09

问题


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
   };

回答1:


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.




回答2:


I tried the selected answer but when querying on specific model object, it retrieves data of both schemas. So I think using discriminator yields better solution:

const coordinateSchema = new Schema({
    lat: String,
    longt: String,
    name: String
}, {collection: 'WeatherCollection'});

const windSchema = new Schema({
   windGust: String,
   windDirection: String,
   windSpeed: String
}, {collection: 'WeatherCollection'});

//Then define discriminator field for schemas:
const baseOptions = {
    discriminatorKey: '__type',
    collection: 'WeatherCollection'
};

//Define base model, then define other model objects based on this model:
const Base = mongoose.model('Base', new Schema({}, baseOptions));
const CoordinateModel = Base.discriminator('CoordinateModel', coordinateSchema);
const WindModel = Base.discriminator('WindModel', windSchema);

//Query normally and you get result of specific schema you are querying:
mongoose.model('CoordinateModel').find({}).then((a)=>console.log(a));

Example output:

{ __type: 'CoordinateModel', // discriminator field
    _id: 5adddf0367742840b00990f8,
    lat: '40',
    longt: '20',
    name: 'coordsOfHome',
    __v: 0 },


来源:https://stackoverflow.com/questions/14453864/use-more-than-one-schema-per-collection-on-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!