[英]Mongoose self nesting
I'd like to store the following: 我想存储以下内容:
var TestSchema = new Schema({
name: {
type: String
},
regions: [RegionSchema]
}
var RegionSchema = new Schema({
name: {
type: String
},
minX: {
type: Number
},
minY: {
type: Number
},
minZ: {
type: Number
},
maxX: {
type: Number
},
maxY: {
type: Number
},
maxZ: {
type: Number
},
children: [this]
});
So as you can see I'm trying to make a region object, able to contain region objects, however this fails to save without exception, presumably the [this] in the schema gets stuck in some endless loop or something. 因此,正如您所看到的,我正在尝试制作一个能够包含区域对象的区域对象,但是这毫无例外地无法保存,大概是架构中的[this]陷入了一些无休止的循环或某种循环中。
How can I make this to work for nesting regions? 如何使它适用于嵌套区域?
The json payload I could expect to send to this schema: 我可以期望发送到此架构的json负载:
name: "test123",
regions: [
{
name: "TestRegion",
minX: 0,
minY: 0,
minZ: 0,
maxX: 100,
maxY: 255,
maxZ: 100,
children: [
{
name: "TestRegionChild",
minX: 3,
minY: 3,
minZ: 3,
maxX: 97,
maxY: 252,
maxZ: 97,
children: []
}
]
}
]
I appreciate any help, thank you. 感谢您的帮助,谢谢。
If your other regions are saved first, you can save the children as a list of objectIds like this: 如果首先保存其他区域,则可以将子级另存为objectId列表,如下所示:
children: [{
type: Schema.ObjectId,
ref: 'Region'
}]
Another option is to redesign your Schema and have a 'parent' on your Region and delete the 'children' field. 另一个选择是重新设计架构,并在“区域”上有一个“父级”,然后删除“子级”字段。 Then when you need to get all the children you can query easily and not have big nested objects. 然后,当您需要获取所有子项时,您可以轻松查询并且没有大的嵌套对象。
var RegionSchema = new Schema({
--- other fields omitted ---
parent: {
type: Schema.ObjectId,
ref: 'Region'
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.