I'm trying to use $cond
to conditionally $push
multiple integers onto a numbers array during an aggregate $group
without any success. Here is my code:
Item.aggregate(
[
{
$group: {
_id: "$_id",
numbers: {
$push: {
$cond: {
if: { $gt: [ "$price.percent", 70 ] },
then: { $each: [10,25,50,70] },
else: null,
}
}
}
}
},
]
)
...
Is Mongo DB just not set up for this right now, or am I looking at this all wrong?
With aggregate + $match, you get a big monolithic BSON containing all matching documents. With find, you get a cursor to all matching documents. Then you can get each document one by one.
The aggregation query takes ~80ms while the find query takes 0 or 1ms.
The pipeline provides efficient data aggregation using native operations within MongoDB, and is the preferred method for data aggregation in MongoDB. The aggregation pipeline can operate on a sharded collection. The aggregation pipeline can use indexes to improve its performance during some of its stages.
Please try it without $each
as below
Item.aggregate(
[{
$group: {
_id: "$_id",
numbers: {
$push: {
$cond: {
if: { $gt: [ "$price.percent", 70 ] },
then: [10,25,50,70] ,
else: null,
}
}
}
}
}]);
Provided answers will work but they'll add null
to the array whenever else
block gets executed & at the end you need to filter out the null
values from the actual array (numbers
) which is an additional step to do!
You can use $$REMOVE to conditionally exclude fields in MongoDB's $project
.
Item.aggregate(
[{
$group: {
_id: "$_id",
numbers: { $push: { $cond: [{ $gt: ["$price.percent", 70] }, [10, 25, 50, 70], '$$REMOVE'] } } // With $$REMOVE nothing happens on else
}
}]);
REF: $cond
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With