Node.js 如何实现模型本身的嵌套-Mongodb&;航行

Node.js 如何实现模型本身的嵌套-Mongodb&;航行,node.js,mongodb,sails.js,Node.js,Mongodb,Sails.js,到目前为止,我已经了解了这3种型号: 用户模型 地图模型 点模型 因此,如果我按用户查询地图,我将得到,即: { "owner": "1", "id": "1", "spots": [ { "map": "1", "title": "Test Spot", "content_text": "asdasdasdasdasd", "createdAt": "2015-07-14T15:39:50.066Z", "upda

到目前为止,我已经了解了这3种型号:

用户模型

地图模型

点模型

因此,如果我按用户查询地图,我将得到,即:

{
  "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"
        },
所以,基本上,在一张地图中,我希望有多个“起始”点,它们可以有嵌套的级别。我在搜索一些东西,但只能找到与树相关的示例,它们只在左侧和右侧,我希望有两个以上的选项


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

您的模型是正确的。这是因为SAIL目前不支持嵌套模型的填充。您可以这样做来覆盖默认查询

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