我正在尝试使用聚合框架,但我有一个问题。我需要知道我的数据库中有多少人在上个月购买了东西。
为此,我使用这个:
db.account.aggregate([
{$project : {civility : 1, 'purchase.date' : 1 }},
{$match: {civility : 1 ,'purchase.date': {$gte: new Date('02/02/2013'), $lt: new Date('02/03/2013')} }},
{$unwind: '$purchase'},
{$match: {civility : 1 ,'purchase.date': {$gte: new Date('02/02/2013'), $lt: new Date('02/03/2013')} }},
{$group: {_id: '$_id', total_buy : {$sum : 1}}},
{$match: {total_buy: {$gte: 2}}},
{$group: {_id: null, total_buyer : {$sum : 1}}}
])
我有这个回应
{
"result" : [
{
"_id" : null,
"total_buyer" : 4443
}
],
"ok" : 1
}
此查询有效,因为我使用的日期范围很小,但如果我使用相同的查询,日期范围更大,如下所示:
db.account.aggregate([
{$project : {civility : 1, 'purchase.date' : 1 }},
{$match: {civility : 1 ,'purchase.date': {$gte: new Date('02/01/2013'), $lt: new Date('03/01/2013')} }},
{$unwind: '$purchase'},
{$match: {civility : 1 ,'purchase.date': {$gte: new Date('02/01/2013'), $lt: new Date('03/01/2013')} }},
{$group: {_id: '$_id', total_buy : {$sum : 1}}},
{$match: {total_buy: {$gte: 2}}},
{$group: {_id: null, total_buyer : {$sum : 1}}}
])
我有这个 :
{
"errmsg" : "exception: sharded pipeline failed on shard shard0000: { errmsg: \"exception: aggregation result exceeds maximum document size (16MB)\", code: 16389, ok: 0.0 }",
"code" : 16390,
"ok" : 0
}
有什么我做错了或者我不能做我需要做的事情吗?
提前致谢