I have few questions about new aggregation framework.
Question 1. Does non-existence of a field considered as $min value in a $group?
For e.g. In the following script, min_metric2 is not printed because at-least one document doesn't have it. How do I find the min value among the remaining documents?
db.metric.remove({});
db.metric.save({year: 2012, month: 1, metrics: {metric1: 30, metric2: 80}});
db.metric.save({year: 2012, month: 2, metrics: {metric1: 50, metric2: 20}});
db.metric.save({year: 2012, month: 3, metrics: {metric1: 10}});
db.metric.save({year: 2012, month: 4, metrics: {metric1: 20, metric2: 40}});
db.metric.save({year: 2012, month: 5, metrics: {metric1: 40, metric2: 60}});
var result = db.metric.aggregate
(
[
{
$group: {
_id: {year: "$year"},
min_metric1: {$min: "$metrics.metric1"},
min_metric2: {$min: "$metrics.metric2"},
max_metric1: {$max: "$metrics.metric1"},
max_metric2: {$max: "$metrics.metric2"},
}
}
]
);
printjson(result);
Question 2: How do I set a fixed String (hard-coded value) to a field in $project or anywhere else in the pipeline?
Question 3: Is it possible to define a field conditionally (based on a computed value) in $project?
Thank you.