[英]How do I set a different type of property in mongoose schema from the defined type?
[英]How do you set a schema property to be of type SubDocument in Mongoose?
我想做這樣的事情:
var userSchema = new Schema({
local: localSchema,
facebook: facebookSchema,
twitter: twitterSchema,
google: googleSchema
});
但是似乎Schema不是有效的SchemaType 。
在SubDocuments 指南中 ,他們僅提供一個示例,其中將子架構放在數組中,但這不是我想要的。
var childSchema = new Schema({ name: 'string' });
var parentSchema = new Schema({
children: [childSchema]
})
看來您只是想為每個屬性創建一個子對象。 您可以通過以下兩種方法之一來完成此操作。
var userSchema = new Schema({
local: {
someProperty: {type: String}
//More sub-properties...
}
//More root level properties
});
//this could be defined in a separate module and exported for reuse
var localObject = {
someProperty: {type: String}
//more properties
}
var userSchema = new Schema({
local: localObject
});
var someOtherSchema = new Schema({
test: localObject
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.