过滤子列

时间:2016-09-15 16:10:13

标签: mongodb mongodb-aggregation

我有一个集合:

[
  {
    "name": "Alex",
    "cars": [
      {
        "label": "BMW",
        "age": 13,
        "things": [ ... ]
      },
      {
        "label": "Mercedes",
        "age": 8,
        "things": [ ... ]
      }
    ]
  },
  { ... }
]

我会过滤掉每个人things的{​​{1}}个。因此,获取相同的集合(具有相同的模式),但没有car s。

我可以通过MongoDB聚合来做到这一点吗?

UPD:“可能重复”解释了如何过滤子阵列而不是子阵列(AFAIK)。

UPD2:我写了一些代码来过滤子列,但我应该指出我要保存的所有字段......

thing

问题:我可以自动保留db.matches.aggregate([{ $project: { '_id': '$_id', 'cars': { $map: { input: '$cars', as: 'car', in: { 'things': { $filter: { input: '$$car.things', as: 'thing', cond: { $lte: [ '$$thing.cost', 10 ] } } } } } } } } ]) 及其person的其他字段(因为现实世界中有很多字段)吗?

0 个答案:

没有答案