如何实现模型本身的嵌套 – Mongodb&Sails

到目前为止,我已经明白了这三种模式:

用户模型

module.exports = { schema: true, attributes: { // Relations maps: { collection: 'Map' }, } }; 

地图模型

 module.exports = { schema: true, attributes: { // Relations owner: { model: 'User' }, spots: { collection: 'Spot', via: 'map' }, } }; 

现货型号

 module.exports = { schema: true, attributes: { // Relations map: { model: 'Map' }, parentSpot: { model: 'Spot' }, subSpotss: { collection: 'Spot', via: 'parentSpot' }, } }; 

所以,如果我查询一个地图,用户,我会得到,即:

 { "owner": "1", "id": "1", "spots": [ { "map": "1", "title": "Test Spot", "content_text": "asdasdasdasdasd", "createdAt": "2015-07-14T15:39:50.066Z", "updatedAt": "2015-07-14T15:39:50.066Z", "id": "1" }, { "map": "1", "title": "Another Spot", "content_text": "hue hue hue hue hue", "createdAt": "2015-07-14T15:40:17.313Z", "updatedAt": "2015-07-14T15:40:17.313Z", "id": "2" } ], "createdAt": "2015-07-14T15:38:32.571Z", "updatedAt": "2015-07-14T15:38:32.571Z" } 

我想要的,另外,是embedded其他景点,所以我有这样的结果:

 { "owner": "1", "id": "1", "spots": [ { "map": "1", "title": "Test Spot", "content_text": "asdasdasdasdasd", "spots": [ { "map": "1", "title": "Nested Spot", "content_text": "dfgsdsfasdf", "spots": [ { "map": "1", "title": "Another Nested Spot", "content_text": "sometesxtisdjfiasj", "spots": [ { // more nested levels here }, { // more nested levels here }, ], "createdAt": "2015-07-14T15:39:50.066Z", "updatedAt": "2015-07-14T15:39:50.066Z", "id": "5" }, { // more nested levels here }, ], "createdAt": "2015-07-14T15:39:50.066Z", "updatedAt": "2015-07-14T15:39:50.066Z", "id": "3" }, { // another nested Spot which can have a collections of // more Nested spots }, { // one more nested Spot which can have a collections of // more Nested spots } ], "createdAt": "2015-07-14T15:39:50.066Z", "updatedAt": "2015-07-14T15:39:50.066Z", "id": "1" }, 

所以,基本上,在一个Map中,我想要有多个“开始” Spot ,它们可以嵌套在其中。 我正在寻找一些东西,但是只能find与树有关的例子,这些例子只能左右 移动 ,我想要有两个以上的选项。

我如何在Sails模型中编写? 这是可行的吗? build议更好的devise是值得欢迎的。

你的模型是正确的。 这是因为sails目前不支持在嵌套模型中填充。 您可以像在这个解决scheme中那样重写默认查询。