Skip to content Skip to sidebar Skip to footer

Mongoose - Aggregation $match Based On Enum Values

I have an aggregation with mongoose, the objects what i get looks like this: foods: { fruits: { apple: 'NONE', banana: null, pearl: 'INCLUDED' } } The fruits can

Solution 1:

You can try below aggregation in mongodb 3.4.4 and above

Well you have unknown keys here having value with NONE and null and therefore, you can use $objectToArray aggregation to make keys into values and can easily $match with them

db.collection.aggregate([
  { "$addFields": {
    "data": { "$objectToArray": "$foods.fruits" }
  }},
  { "$match": { "data.v": { "$in": [ "NONE", null ] } } },
  { "$project": { "data": 0 }}
])

Post a Comment for "Mongoose - Aggregation $match Based On Enum Values"