在mongodb上为每个集合使用多个模式

时间:2013-01-22 07:50:58

标签: node.js mongodb mongoose schema-design nosql

我想在mongodb中为每个集合使用多个模式,如何使用它....? 当我尝试运行它时,它给了我这个错误:

错误:

  

OverwriteModelError:编译后无法覆盖allUsers模型。   
OverwriteModelError:编译后无法覆盖checkInOut模型。

左下我的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
   };

2 个答案:

答案 0 :(得分:49)

在猫鼬中你可以这样做:

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

这两个模式将保存在'用户'集合中。

有关详细信息,请参阅文档:http://mongoosejs.com/docs/api.html#index_Mongoose-model,或者您可以看到以下gist可能会有所帮助。

答案 1 :(得分:2)

我尝试了所选答案,但在查询特定模型对象时,它会检索两个模式的数据。所以我认为使用鉴别器会产生更好的解决方案:

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

示例输出:

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