I want to use a pipeline to update a document by filtering on the keys of sub-documents in an object.
eg data:
{
things: {
a1: {},
...
x1: {}
}
}
Where "things" can contain variable keys/properties. I know I can use an update pipeline like this, where "keepKeys" are the keys I want to keep in the "things" object:
[
$set: {
things: {
$arrayToObject: {
$filter: {
"input": {
$objectToArray: "$$ROOT.things"
},
"as": "item",
"cond": {
"$in": [
"$$item.k",
keepKeys
]
}
}
}
}
]
This works, but I'd rather filter out the things I don't want as this is a much shorter list, eg where discardKeys are the keys I don't want:
[
$set: {
things: {
$arrayToObject: {
$filter: {
"input": {
$objectToArray: "$$ROOT.things"
},
"as": "item",
"cond": {
"$nin": [
"$$item.k",
discardKeys
]
}
}
}
}
]
But $nin is not supported in pipeline $filter cond
This works, as per @Wernfrie Domscheit
[
$set: {
things: {
$arrayToObject: {
$filter: {
"input": {
$objectToArray: "$$ROOT.things"
},
"as": "item",
"cond": {
"$not": {
"$in": [
"$$item.k",
discardKeys
]
}
}
}
}
}
]