我正在学习mongodb。我有一个看起来像这样的文件:
{
"_id" : ObjectId("558bb3490e98e0108940afee"),
"term" : "nike team court graphic mens tennis crew and shorts",
"year" : "2015",
"month" : "06",
"day" : "06",
"hour" : "12",
"count" : 15
}
{
"_id" : ObjectId("558bb3500e98e0108940afef"),
"term" : "nike team court graphic mens tennis crew and shorts",
"year" : "2015",
"month" : "06",
"day" : "06",
"hour" : "12",
"count" : 12
}
{
"_id" : ObjectId("558bb3550e98e0108940aff0"),
"term" : "nike team court graphic mens tennis crew and shorts",
"year" : "2015",
"month" : "06",
"day" : "06",
"hour" : "12",
"count" : 18
}
{
"_id" : ObjectId("558bb3640e98e0108940aff1"),
"term" : "nike",
"year" : "2015",
"month" : "06",
"day" : "06",
"hour" : "12",
"count" : 18
}
{
"_id" : ObjectId("558bb3680e98e0108940aff2"),
"term" : "nike",
"year" : "2015",
"month" : "06",
"day" : "06",
"hour" : "12",
"count" : 12
}
{
"_id" : ObjectId("558bb36b0e98e0108940aff3"),
"term" : "nike",
"year" : "2015",
"month" : "06",
"day" : "06",
"hour" : "12",
"count" : 13
}
{
"_id" : ObjectId("558bb3720e98e0108940aff4"),
"term" : "nike",
"year" : "2015",
"month" : "06",
"day" : "06",
"hour" : "12",
"count" : 10
}
{
"_id" : ObjectId("558bb3790e98e0108940aff5"),
"term" : "nike1",
"year" : "2015",
"month" : "06",
"day" : "06",
"hour" : "12",
"count" : 10
}
{
"_id" : ObjectId("558bb37e0e98e0108940aff6"),
"term" : "nike1",
"year" : "2015",
"month" : "06",
"day" : "06",
"hour" : "12",
"count" : 111
}
{
"term" : "nike1",
"year" : "2014",
"month" : "03",
"day" : "07",
"count" : 110
}
我希望以这样的方式执行聚合,即返回期限,年,月,天和总计数总和基于期限,年,月和日的匹配。
我的查询
db.products.group( {
$match:{$and:[{year:"2015"},{month:"06"},{day:"06"}]},
key: { term: "$term", year: "2015", month : "06", day : "06" },
reduce: function(cur, result) { result.count += cur.count },
initial: { count: 0 }
} )
我得到的输出:
[
{
"term" : "nike team court graphic mens tennis crew and shorts",
"year" : "2015",
"month" : "06",
"day" : "06",
"count" : 45
},
{
"term" : "nike",
"year" : "2015",
"month" : "06",
"day" : "06",
"count" : 53
},
{
"term" : "nike1",
"year" : "2015",
"month" : "06",
"day" : "06",
"count" : 121
},
{
"term" : "nike1",
"year" : "2014",
"month" : "03",
"day" : "07",
"count" : 110
}
]
最后的结果不是预期的
{
"term" : "nike1",
"year" : "2014",
"month" : "03",
"day" : "07",
"count" : 110
}
因为年,月和日并没有完全填满我通过的条件。 我知道我错过了什么。有人可以指出来吗?
答案 0 :(得分:1)
试试这样:
db.products.aggregate([ { $match: { year: "2015", month: "06", day: "06" } }, { $group: { _id: { year: "$year", month: "$month", day: "$day", term: "$term" } count: { $sum: "$count" } } } ])
答案 1 :(得分:1)
使用聚合
解决了这个问题db.products.aggregate([
{$match:
{$and:[
{year:"2015"},
{month:"06"},
{day:"06"}]}},
{$group:{
_id : "$year",
_id : "$month",
_id : "$day",
_id :"$term",
totalcount : {
$sum : "$count"
}
}
}
])
答案 2 :(得分:1)
根据this,db.collection.group()命令接受字段cond
作为选择标准,而不是$match
。尝试一下。
答案 3 :(得分:1)
如果您只想按年,月和日分组:
db.products.aggregate([{
$group: {
_id: { year:"$year", month:"$month", day:"$day"},
sumOfCount: { $sum:"$count" }
}
}])
如果你想过滤:
db.products.aggregate([{
$match: {
year: "2015",
month: "06",
day: "06"
}
}, {
$group: {
_id: { year:"$year", month:"$month", day:"$day"},
sumOfCount: { $sum:"$count" }
}
}])