如何实现模型本身的嵌套 - Mongodb&帆

时间:2015-07-14 19:26:48

标签: node.js mongodb sails.js

到目前为止,我已经了解了这3个模型:

用户模型

module.exports = {
    schema: true,

    attributes: {
        // Relations
        maps: {
            collection: 'Map'
        },
    }
};

地图模型

module.exports = {
    schema: true,

    attributes: {
        // Relations
        owner: {
            model: 'User'
        },

        spots: {
            collection: 'Spot',
            via: 'map'
        },
    }
};

Spot 模型

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"
}

另外,我想要的是在斑点内嵌套其他斑点,所以我有这样的结果:

{
      "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 ,可以在其中包含嵌套级别。我正在寻找一些东西,但只能找到与树有关的例子,它只有,我希望有两个以上的选项。

如何在Sails模型中编写?这是否可行?欢迎提出更好的设计建议。

1 个答案:

答案 0 :(得分:1)

你的模型是对的。这是因为sails目前不支持在嵌套模型中填充。您可以通过执行此solution

来覆盖默认查询