下面的查询给出了每年模型的总和。我需要找到最大模型的年份。 (数据是制造商中的另一个数组)
var config = {headers : {
"Content-Type": "application/json; charset = utf-8;"
}
};
答案 0 :(得分:1)
现在你将获得最大型号的年份。
所以你的Query(Java实现)将如下所示:
// Group by Year
DBObject groupFields = new BasicDBObject( "_id", "$_id.year");
//Count total no of models made in that year
groupFields.put("sum", new BasicDBObject( "$sum", "$manufacturers.data.modelName"));
//Group the fields
DBObject group = new BasicDBObject("$group", groupFields);
//Sort in descending order inorder to get maximum models
DBObject sortFields = new BasicDBObject("sum", -1);
DBObject sort = new BasicDBObject("$sort", sortFields );
//Limit to one since you need the year when maximum models were made
DBObject limit = new BasicDBObject("$limit", 1 );
AggregationOutput output = myColl.aggregate(group,sort,limit);
希望这有帮助。
编辑输入:
db.structure.aggregate([
{$unwind : "$manufacturers"},
{$group: {
_id: {
day:"$_id.year"
},
count: { $sum: "$manufacturers.data.modelName" }
}},
{$sort: {count : -1}},
//Limit by 1 to get year when maximum models where made
{$limit : 1},
{$out : "out"}
]);