猫鼬中相同集合的不同模式

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。要使用arraynumber 类型,您应该将类​​型定义为Mixed

var infoSchema = new models.Schema(
    productnumber: ,
    repairshopnumber: mongoose.Schema.Types.Mixed,
    reservationtimes: [],
    currenttime: Date,
    liftposition: Number,
    user_token: String
);

警告:对于这种类型,数字可能存储在字符串中

【讨论】:

谢谢你,我解决了问题

以上是关于猫鼬中相同集合的不同模式的主要内容,如果未能解决你的问题,请参考以下文章

是否有一种解决方案允许从猫鼬中完全相同的快照中读取多个集合?

如何将相同的模式引用为猫鼬中的类型[重复]

猫鼬中嵌套文档的不同类型是啥?

关于如何在猫鼬中编写评论模式的任何想法?

关于如何在猫鼬中编写评论模式的任何想法?

在猫鼬中,如何根据相关集合中的值查找记录?