1

I have a json array structured like this: { "properties": [{ "name": "ok", "value": 1 }, { "name": "ok_aswell", "value": 1 }, { "name": "ok_aswell", "value": 2 }, { "name": "get_rid", "value": 2 } ] }

I want my query to return everything except the "get_rid" record: { "properties": [{ "name": "ok", "value": 1 }, { "name": "ok_aswell", "value": 1 }, { "name": "ok_aswell", "value": 2 } ] }

I tried the suggestions from here: Conditionally include a field (_id or other) in mongodb project aggregation? , however they didn't seem to work.

davo777
  • 286
  • 2
  • 15

1 Answers1

2

Try $filter in projection aggregate

{$project:{
  properties:{
      $filter: {
                 input: "$properties",
                 as: "item",
                 cond: {$ne: ["$$item.name","get_rid"]}
               } 
  }
}}
sushant mehta
  • 1,244
  • 1
  • 7
  • 13