2016-05-07 54 views
0

我在貓鼬下面的模式,我想有作爲另一個模式的嵌套陣列:定義基於貓鼬架構對象的陣列

var NestedSchema = new Schema({ 
    name: { type: String, required: true } 
}); 

需要和嵌套的陣列的一些其他架構架構。

var EventSchema = new Schema({ 
    name: { type: String, required: true, unique: true }, 
    fields: [NestedSchema] 
}); 

這工作得很好。但是現在我想對該數組運行一些驗證。

var validators = // some validators 
var EventSchema = new Schema({ 
    name: { type: String, required: true, unique: true }, 
    fields: [{ type: 'NestedSchema', required: true, validate: validators }] 
}); 

當然類型:'NestedSchema'不起作用,它是在黑暗中拍攝。貓鼬是否允許您根據架構

回答

1

使用有對象的數組: MongoDB的外殼版本:3.2.12, 貓鼬:4.4.7

我管理的基於模式這種方式有一個數組:

var NestedSchema = new Schema({ 
    name: { type: String, required: true } 
}); 

var EventSchema = new Schema({ 
    name: { type: String, required: true, unique: true }, 
    fields: [{ type: [NestedSchema], required: true }] 
}); 

沒有嘗試驗證,但我相信它會按要求運作良好。 :)