MongoDB - Is it possible to use $or with $cond?
aggregation-framework, mongodb, nosql
Solution
I believe I got it using $and
db.project.aggregate(
{ $match : { brand : 4 } },
{
$project : {
_id : 0,
brand : 1,
count : 1,
day : 1
}
},
{
$group : {
_id : "$brand",
TotalCount : { $sum : "$count" },
TimeRangeCount : {
$sum: {
$cond: [
{$and: [
{$gte: ["dDay", new Date(2014, 0, 6)]},
{$lte: ["$day", new Date(2014, 0, 8)]}
]},
"$count",
0
]
}
}
}
}
)
Problem
I am trying to aggregate a field based on a date range. I can't figure out how to create a $cond expression that will evaluate more than one expression. I am trying to use the logical $or operator but with no luck. ``` db.project.aggregate( { $match : { brand : 4 } }, { $project : { _id : 0, brand : 1, count : 1, day : 1} }, { $group : { _id : "$brand", TotalCount : { $sum : "$count" }, TimeRangeCount : {$sum: { $cond: [ { $gte: [ "$day", new Date(2014, 0, 6) ], {$or: [$lte: [ "$day", new Date(2014, 0, 8) ]]} }, "$count", 0 ] }} }} ) ``` Is it possible to nest $or within $cond? Thanks!