DB version 4.4
Kotlin driver: kotlin-sync:4.11.0
I have a collection where the document has a field of type array. In my aggregation pipeline I want to add a new field when a filed of the array members is true:
{
...
"existingField": [
{..., "primary": true, ...},
{..., "primary": false, ...}
],
...
}
In MongoDB Compass it is simply this: (and works fine)
{
newField: {
$filter: {
input: "$existingField",
as: "ent",
cond: {
$eq: ["$$ent.primary", true],
},
},
},
}
However, in Kotlin the Aggregation build for this stage looking like this, throws a error.
fun onlyArrayMemberPrimaryValuesTrue(): Bson = Aggregates.set(
Field("newField", Filters.elemMatch("existingField",
Filters.eq("primary", true))
)
)
I have tried a bunch of variations and gotten no luck, I also cannot find any examples documented on the way these should be appropriately built. What am I missing here?