node.js - 如何实现模型本身的嵌套 - Mongodb & Sails

标签 node.js mongodb sails.js

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

用户模型

module.exports = {
    schema: true,

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

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'
        },
    }
};

所以,如果我按用户查询 map ,我会得到,即:

{
  "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 内,我想要有多个“起始”地点,其中可以有嵌套的级别。我正在寻找一些东西,但只能找到与树相关的示例,这些示例只能,而我希望有两个以上的选项。

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

最佳答案

你的模型是正确的。这是因为 sails 目前不支持在嵌套模型中填充。您可以通过这样做来覆盖默认查询 solution .

关于node.js - 如何实现模型本身的嵌套 - Mongodb & Sails,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31415596/

相关文章:

javascript - Google Text-toSpeech - 在前端获取音频文件

python - 使用 MongoEngine 动态文档将 None 保存到 MongoDB 中

javascript - Sails.js:在我想发送实际响应之前发送了 200 响应

javascript - 使用 ng-repeat 同步数据库中请求的数据

node.js - 使用软件包安装程序安装node.js-dyld :Library not loaded error

javascript - 错误 : Can't set headers after they are sent to the client

c++ - 为什么 find_one 不能在 MongoDB C++ 中工作?

php - 排序在新的 mongodb PECL 扩展中是如何工作的?

node.js - 如何检查 Node.js 中的函数是同步运行还是异步运行

node.js - 在 View 中显示一对多关系的列表 sails.js waterline orm