我使用的是mongoDb 2.6.4,但仍然有一个错误:
uncaught exception: aggregate failed: {
"errmsg" : "exception: aggregation result exceeds maximum document size (16MB)",
"code" : 16389,
"ok" : 0,
"$gleStats" : {
"lastOpTime" : Timestamp(1422033698000, 105),
"electionId" : ObjectId("542c2900de1d817b13c8d339")
}
}阅读不同的建议,我发现保存结果在另一个集合使用$out。我的查询现在如下所示:
db.audit.aggregate([
{$match: { "date": { $gte : ISODate("2015-01-22T00:00:00.000Z"),
$lt : ISODate("2015-01-23T00:00:00.000Z")
}
}
},
{ $unwind : "$data.items" } ,
{
$out : "tmp"
}]
)但我得到了不同的错误:未提及的异常:聚合失败:
{"errmsg" : "exception: insert for $out failed: { lastOp: Timestamp 1422034172000|25, connectionId: 625789, err: \"insertDocument :: caused by :: 11000 E11000 duplicate key error index: duties_and_taxes.tmp.agg_out.5.$_id_ dup key: { : ObjectId('54c12d784c1b2a767b...\", code: 11000, n: 0, ok: 1.0, $gleStats: { lastOpTime: Timestamp 1422034172000|25, electionId: ObjectId('542c2900de1d817b13c8d339') } }",
"code" : 16996,
"ok" : 0,
"$gleStats" : {
"lastOpTime" : Timestamp(1422034172000, 26),
"electionId" : ObjectId("542c2900de1d817b13c8d339")
}
}有人能找到解决办法吗?
发布于 2015-01-23 18:02:57
此错误是由于管道中的$unwind步骤造成的。
当您通过具有unwind元素的字段进行n时,使用相同的_id生成相同文档的n副本。每个副本都具有用于unwind的数组中的一个元素。请参阅下面unwind操作后记录的演示。
示例演示:
> db.t.insert({"a":[1,2,3,4]})
WriteResult({ "nInserted" : 1 })
> db.t.aggregate([{$unwind:"$a"}])
{ "_id" : ObjectId("54c28dbe8bc2dadf41e56011"), "a" : 1 }
{ "_id" : ObjectId("54c28dbe8bc2dadf41e56011"), "a" : 2 }
{ "_id" : ObjectId("54c28dbe8bc2dadf41e56011"), "a" : 3 }
{ "_id" : ObjectId("54c28dbe8bc2dadf41e56011"), "a" : 4 }
>由于所有这些文档都具有相同的_id,所以在插入到名为tmp的新集合时,会得到一个重复的键异常(由于所有未缠绕的文档在_id字段中的值相同)。
如果管道生成的文档违反任何唯一的索引,包括原始输出集合的_id字段上的索引,管道将无法完成。
要解决原来的问题,可以将allowDiskUse选项设置为true。它允许在需要的时候使用磁盘空间。
可选。允许写入临时文件。当设置为true时,聚合操作可以将数据写入dbPath目录中的dbPath子目录。有关示例,请参见使用外部排序执行大型排序操作。
如:
db.audit.aggregate([
{$match: { "date": { $gte : ISODate("2015-01-22T00:00:00.000Z"),
$lt : ISODate("2015-01-23T00:00:00.000Z")
}
}
},
{ $unwind : "$data.items" }] , // note, the pipeline ends here
{
allowDiskUse : true
});https://stackoverflow.com/questions/28115480
复制相似问题