Reputation: 7906
In my query, I have got data like
{
"name": "test",
"prods": [
{
"consider": true,
"recom": true
},
{
"consider": true,
"recom": false
},
{
"consider": false,
"recom": false
}
]
}
In projection, I want to add a field like main_recom: true/false
true if all consider value which is true has recom as true as well.
flase if all consider which is true has any recom as false or if all consider value is false.
Programatically
main_recom = false
foreach prod of prods
if prod.consider = true:
if prod.recom = true:
main_recom = true
if prod.recom = false:
main_recom = false
break; // break loop
Upvotes: 0
Views: 41
Reputation: 36114
You can use aggregation operators,
$concatArrays
to concat both the property's value in a single array$allElementsTrue
to check if the above array has all values true then return true otherwise falsedb.collection.aggregate([
{
$addFields: {
main_recom: {
$allElementsTrue: {
$concatArrays: ["$prods.consider", "$prods.recom"]
}
}
}
}
])
Upvotes: 1