0

I have a collection:

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

I would to filter out thingss of each car of each person. So, get the same collection (with the same schema) but without some of things.

Ho do I do this via MongoDB aggregations?

UPD: The "possible duplicate" explains how to filter subarray but not subsubarray in it (AFAIK).

UPD2: I wrote some code that filter subsubarray but I should point out all fields I want to save...

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 ]
              }
            }
          }
        }
      }
    }
  }
}
])

Question: can I automatically preserve other fields for persons and their cars (because there are many fields in real world)?

Kuraga
  • 331
  • 3
  • 16
  • Possible duplicate of [How to filter array in subdocument with MongoDB](http://stackoverflow.com/questions/15117030/how-to-filter-array-in-subdocument-with-mongodb) – Alex Blex Sep 15 '16 at 16:39
  • @AlexBlex , no, but it shows how to start to solve the problem. Thanks! – Kuraga Sep 15 '16 at 18:06

0 Answers0