mongoDB 查询结果
mongoDB query result
我正在学习 mongodb.I 有一份如下所示的文档:
{
"_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
}
我想以 returns term , year , month , day and sum of total count based on the match of term,year,month and day.
我的查询
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
}
因为年月日没有完全满足我通过的条件。
我知道我遗漏了 something.Can 谁能指出来?
这样试试:
db.products.aggregate([
{
$match: {
year: "2015",
month: "06",
day: "06"
}
},
{
$group: {
_id: {
year: "$year",
month: "$month",
day: "$day",
term: "$term"
}
count: {
$sum: "$count"
}
}
}
])
使用聚合解决了这个问题
db.products.aggregate([
{$match:
{$and:[
{year:"2015"},
{month:"06"},
{day:"06"}]}},
{$group:{
_id : "$year",
_id : "$month",
_id : "$day",
_id :"$term",
totalcount : {
$sum : "$count"
}
}
}
])
根据 this,db.collection.group() 命令接受字段 cond
作为选择标准,而不是 $match
。试试吧。
如果您只想按年月日分组,那么:
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" }
}
}])
我正在学习 mongodb.I 有一份如下所示的文档:
{
"_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
}
我想以 returns term , year , month , day and sum of total count based on the match of term,year,month and day.
我的查询
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
}
因为年月日没有完全满足我通过的条件。 我知道我遗漏了 something.Can 谁能指出来?
这样试试:
db.products.aggregate([ { $match: { year: "2015", month: "06", day: "06" } }, { $group: { _id: { year: "$year", month: "$month", day: "$day", term: "$term" } count: { $sum: "$count" } } } ])
使用聚合解决了这个问题
db.products.aggregate([
{$match:
{$and:[
{year:"2015"},
{month:"06"},
{day:"06"}]}},
{$group:{
_id : "$year",
_id : "$month",
_id : "$day",
_id :"$term",
totalcount : {
$sum : "$count"
}
}
}
])
根据 this,db.collection.group() 命令接受字段 cond
作为选择标准,而不是 $match
。试试吧。
如果您只想按年月日分组,那么:
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" }
}
}])