davo777
davo777

Reputation: 336

Omitting part of a mongo array when it has a specific value

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.

Upvotes: 1

Views: 34

Answers (1)

sushant mehta
sushant mehta

Reputation: 1274

Try $filter in projection aggregate

{$project:{
  properties:{
      $filter: {
                 input: "$properties",
                 as: "item",
                 cond: {$ne: ["$$item.name","get_rid"]}
               } 
  }
}}

Upvotes: 2

Related Questions