简体   繁体   English

过滤孩子的标签字段

[英]Filter on tag-field on children

First of all, I'm not sure I've set this up as it should be, like by the book.首先,我不确定我是否按照应有的方式进行了设置,就像书中那样。 I'm from the SQL world and jumped into the NOSQL land.我来自 SQL 世界并进入了 NOSQL 领域。

Ok, so.好的,所以。 I have this collection with Projects, and inside the projects I have files as a child-ref.我有这个项目集合,在项目中我有文件作为子引用。 I can populate and all that stuff, works really well.我可以填充所有这些东西,效果非常好。 But I want to filter with tags.但我想用标签过滤。 I have a tags field inside the File collection, an array with strings, pretty straight forward.我在 File 集合中有一个 tags 字段,一个带有字符串的数组,非常简单。

What I would like to do is;我想做的是; send a projectId and a string with a spec filter and get the files, belonging to the project and also containing the tag.发送一个 projectId 和一个带有规范过滤器的字符串,并获取属于项目并包含标签的文件。 Oh, and also, populated.哦,还有,人口稠密。

Is this even the right approach with NOSQL/MONGO?这甚至是使用 NOSQL/MONGO 的正确方法吗? I know how I would do it in SQL, with parent_id's and with some joins etc. I've looked into some aggregate but I'm too novice to work it out it seems.我知道我将如何在 SQL 中使用 parent_id 和一些连接等来完成它。我已经研究了一些聚合,但我似乎太新手了,无法解决它。

edit, just to show how my collections are built:编辑,只是为了展示我的收藏是如何构建的:

Project Collection项目集

[{
  id: 1,
  name: 'Project01',
  files: [
    id: 1,
    id: 2,
    id: 3,
    id: 4,
    id: 5,
    ...
  ]
},
...
]

Files Collection档案收集

[{
  id: 1,
  name: 'filename'
  tags: ['a','b']
},{
  id: 2,
  name: 'filename2'
  tags: ['b', 'c']
},{
  id: 3,
  name: 'filename3'
  tags: ['a', 'd', 'e', 'f']
},
...]

The result I'm going for (get all files in project 1 where tags includes 'b'.我要的结果(获取项目 1 中标签包含“b”的所有文件。

{
  id: 1,
  name: 'Project01',
  files: [
    {
      id: 1,
      name: 'filename'
      tags: ['a','b']
    },{
      id: 2,
      name: 'filename2'
      tags: ['b', 'c']
    }
  ]
}

try this $unwind operation in mongodb Collections as per your requirement根据您的要求在 mongodb Collections 中尝试此$unwind操作

[
  {
    _id: 1,
    name: "Project01",
    files: [
      {
        id: 1,
        name: "filename11",
        tags: [
          "a",
          "b"
        ]
      },
      {
        id: 2,
        name: "filename12",
        tags: [
          "b",
          "c"
        ]
      },
      {
        id: 3,
        name: "filename13",
        tags: [
          "a",
          "c"
        ]
      }
    ]
  },
  {
    _id: 2,
    name: "Project02",
    files: [
      {
        id: 1,
        name: "filename21",
        tags: [
          "a",
          "b"
        ]
      },
      {
        id: 2,
        name: "filename22",
        tags: [
          "a",
          "c"
        ]
      },
      {
        id: 3,
        name: "filename23",
        tags: [
          "b",
          "c"
        ]
      }
    ]
  }
]

Method 1: for your project collection方法一:为你的项目收藏

 db.collection.aggregate([
  {
    $match: {
      _id: 1
    }
  },
  {
    $unwind: "$files"
  },
  {
    $match: {
      _id: 1,
      "files.tags": {
        $in: [
          "b"
        ]
      }
    }
  }
])

Method 2 for files collection文件收集方法二

   db.collection.aggregate([
  {
    $unwind: "$tags"
  },
  {
    $match: {
      tags: "xyz"
    }
  }
])

Try it here Mongoplayground在这里尝试一下Mongoplayground

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM