猫鼬中相同集合的不同模式
Posted
技术标签:
【中文标题】猫鼬中相同集合的不同模式【英文标题】:Different schema for same collections in mongoose 【发布时间】:2016-01-27 21:05:34 【问题描述】:您好,我学习猫鼬的时间很短,所以我有一些问题
我想在同一个集合中使用两个不同的架构 因为有时我只想在repairshopnumber,有时我想arraydata在repairshopnumber
var infoSchema = new models.Schema(
productnumber: ,
repairshopnumber: ,
reservationtimes: [],
currenttime: Date,
liftposition: Number,
user_token: String
);
var ScheduleSchema = new models.Schema(
productnumber: ,
repairshopnumber: Number,
reservationtimes: [],
currenttime: Date,
liftposition: Number,
user_token: String
);
var reservationinfo = models.model('reservations', infoSchema);
var reservationschedule = mongoose.model('reservations', ScheduleSchema);
会发生错误
/home/node/node_modules/mongoose/lib/index.js:334
throw new mongoose.Error.OverwriteModelError(name);
^
OverwriteModelError: Cannot overwrite `reservations` model once compiled.
如何输入?我找不到任何解决方案。
【问题讨论】:
您正在编写具有两个不同模式的相同模型。这就是问题所在。 @Michelem Mongoose 实际上支持一个集合的多个模式:mongoosejs.com/docs/api.html#index_Mongoose-model。但是帖子所有者用错了。 【参考方案1】:无法为您的集合定义两个架构。正确的做法是将所有这些合并到一个架构中。
在您的架构中,唯一的区别是字段repairshopnumber
。要使用array
和number
类型,您应该将类型定义为Mixed
:
var infoSchema = new models.Schema(
productnumber: ,
repairshopnumber: mongoose.Schema.Types.Mixed,
reservationtimes: [],
currenttime: Date,
liftposition: Number,
user_token: String
);
警告:对于这种类型,数字可能存储在字符串中
【讨论】:
谢谢你,我解决了问题以上是关于猫鼬中相同集合的不同模式的主要内容,如果未能解决你的问题,请参考以下文章