{
"offers" : [
{
"name" : "min",
"prices" : [
8, 15, 18
]
},
{
"name" : "max",
"prices" : [
9, 11, 18
]
}
],
"nl" : [
2, 4, 6, 7
]
}
update aggregation not working $in. I have tried on the two ways
[
{
$set: {
offers: {
$cond: {
if: {
"nl": {$in: [7]}
},
then: {
$concatArrays: ["$offers", []]
},
else: {
$concatArrays: ["$offers", [{
"name" : "minimal",
"prices" : []
}]]
}
}
}
}
},
]
The code below is not working and not showing any error.
[
{
$set: {
offers: {
$cond: {
if: {
$in: ["$nl", 7]
},
then: {
$concatArrays: ["$offers", []]
},
else: {
$concatArrays: ["$offers", [{
"name" : "minimal",
"prices" : []
}]]
}
}
}
}
},
]
2
Answers
$in on aggregation is on this syntax: (see MongoDb Docs)
You need to inverse $in condition. Like this:
I’m not sure what your expected outcome is but you need to pass the number 7 as the first argument (a valid expression) and the array to compare against as the second (a valid expression that resolves to an array) like so:
See HERE for a working example.