没有数组的猫鼬上的子模式 [英] Sub-Schemas on Mongoose without Arrays
本文介绍了没有数组的猫鼬上的子模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以,我想知道,即使我知道您不能创建单个子文档,我仍然想创建一个子文档,以便可以正确使用默认和其他猫鼬类型,是否有办法还在做这样的事吗?
So, I was wondering, even though I understood that you cannot create a single sub-document, I still would like to create a sub-document so that I can use default and other mongoose types properly, is there a way to still do such a thing?
例如:
var SomeOtherScheme = new Schema({
a : { type:String, default:'test' },
b : { type:Boolean, default:false }
...
});
var GroupSettings = new Schema({
x : { type:Number, default:20 },
y : { type:Boolean, default:false },
...
else : { type:SomeOtherScheme, default:SomeOtherScheme }
});
var Group = new Schema({
name : { type:String , required:true, unique:true},
description : { type:String, required:true },
...
settings : {type:GroupSettings,default:GroupSettings}
});
推荐答案
嵌入式对象的架构需要使用普通对象进行定义,因此,如果要保持定义的独立性,可以执行以下操作:
The schema of embedded objects need to be defined using plain objects, so if you want to keep the definitions separate you can do it as:
var SomeOther = {
a : { type:String, default:'test' },
b : { type:Boolean, default:false }
...
};
var SomeOtherSchema = new Schema(SomeOther); // Optional, if needed elsewhere
var GroupSettings = {
x : { type:Number, default:20 },
y : { type:Boolean, default:false },
...
else : SomeOther
};
var GroupSettingSchema = new Schema(GroupSettings); // Optional, if needed elsewhere
var GroupSchema = new Schema({
name : { type:String , required:true, unique:true},
description : { type:String, required:true },
...
settings : GroupSettings
});
这篇关于没有数组的猫鼬上的子模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文