Reputation: 190
I have a mongoDB collection as below
{
_id: 1
"probes": ["A", "B"],
"result": ["A", "B","C","D"],
}
I want to compare probes with result and create a new field called exception
{
_id: 1
"probes": ["A", "B"],
"result": ["A", "B","C","D"],
"exception" : ["C","D"]
}
if the exception field is already present, then it has to be overwritten.
Any help is greatly appreciated.
Upvotes: 1
Views: 233
Reputation: 49945
You can use $setDifference operator:
db.collection.aggregate([
{
$addFields: {
exception: {
$setDifference: [ "$result", "$probes" ]
}
}
}
])
Upvotes: 1