MongoDB change stream aggregation and projection on nested variable

39 Views Asked by At

I have a MongoDB collection with documents like below.

{
  "_id": "Omega",
  "Members": [
    {
      "State": false,
      "A" : "Apple"
    },
    {
      "State": true,
      "B" : "Ball"
    }
  ]
}

I would like to filter change stream replace events that match a particular Id and for whom state is true. I already know the aggregation pipeline which I tested in Compass and it works as desired, however I cannot seem to adapt it to the Java Change Streams syntax. Any help would be appreciated. Below is the aggregation query from Compass.

[
  {
    $match: {
      _id: "Omega",
    },
  },
  {
    $project: {
      Members: {
        $filter: {
          input: "$Members",
          as: "item",
          cond: {
            $eq: ["$$item.State", true],
          },
        },
      },
    },
  },
]
0

There are 0 best solutions below