Loopback与其他字段的ReferenceMany类关系

时间:2016-10-05 15:01:27

标签: mongodb loopbackjs embedding

我需要 loopback framework 的帮助。 我有两个模型:Post和Media。 例子:

媒体

{
  id: ObjectId("...a1"), 
  type: "gif",
  path: "some/folder"
},

{
  id: ObjectId("...a2"), 
  type: "mp4",
  path: "some/folder"
},

发表

{
  id: ObjectId("...b1"), 
  title: "Apollo 13",
  content: [
    {
      mediaId: ObjectId("...a1"), 
      header: "header-1",
      description: "descr-1"
    },
    {
      mediaId: ObjectId("...a2"), 
      header: "header-2",
      description: "descr-2"
    }
  ]
},

{
  id: ObjectId("...b2"), 
  title: "2 seconds to Moon",
  content: [
    {
      mediaId: ObjectId("...a1"), 
      header: "header-3",
      description: "descr-3"
    },
  ]
}

你可以猜到我将使用MongoDb。我想描述这两种模型之间的关系,但不确定如何以正确的方式进行。

如果我只有mediaId的数组,我会通过referenceMany。现在它看起来更像embedsMany,但嵌入了很多什么

我甚至尝试制作类似MediaItem模型的内容并为其提供transient数据源。但我没有让它与其他API一起工作。

在最后,我希望获得一个或多个帖子,其中包含媒体数据,例如typepath字段。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可能应该使用HasManyThrough关系(http://loopback.io/doc/en/lb2/HasManyThrough-relations.html),然后包含过滤器(http://loopback.io/doc/en/lb2/Include-filter.html