Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mongodb 如何在不超过最大文档大小的情况下编写聚合?_Mongodb_Pymongo - Fatal编程技术网

Mongodb 如何在不超过最大文档大小的情况下编写聚合?

Mongodb 如何在不超过最大文档大小的情况下编写聚合?,mongodb,pymongo,Mongodb,Pymongo,通过以下查询,我得到了超过最大文档大小问题异常 pipe = [ {"$match": { "birthday":{"$gte":datetime.datetime(1987, 1, 1, 0, 0)} }} ] res =db.patients.aggregate(pipe,allowDiskUse=True) 我通过添加$project操作符修复了它 但是,如果即使我使用$project,文档仍超过16MB,该怎么办 我能做什么?有什么想法吗?多谢各位 pipe = [

通过以下查询,我得到了
超过最大文档大小问题
异常

pipe = [
    {"$match": { "birthday":{"$gte":datetime.datetime(1987, 1, 1, 0, 0)} }}
    ]
res =db.patients.aggregate(pipe,allowDiskUse=True)
我通过添加
$project
操作符修复了它

但是,如果即使我使用
$project
,文档仍超过
16MB
,该怎么办

我能做什么?有什么想法吗?多谢各位

pipe = [
    {"$project": {"birthday":1, "id":1}
    },
    {"$match": { "birthday":{"$gte":datetime.datetime(1987, 1, 1, 0, 0)} }
     }
    ]
res =db.patients.aggregate(pipe,allowDiskUse=True)
例外情况
默认情况下,聚合结果在单个BSON文档中返回给您,这就是大小限制的来源。如果您需要返回的内容超过此范围,您可以:

  • 将结果输出到集合。您可以通过以下方式完成管道:

    {“$out”:“某个集合名称”}

    然后,您可以正常查询该集合(处理完后,您需要自己删除它)

  • 通过在调用聚合时指定
    useCursor=True
    ,将结果作为游标返回

这两个选项都需要mongodb 2.6:如果您仍在运行mongodb 2.4,那么这只是聚合的一个基本限制

使用以下代码段

db.patients.runCommand('aggregate', 
        {pipeline: [
    {"$project": {"birthday":1, "id":1}},
    {"$match": { "birthday":{"$gte":datetime.datetime(1987, 1, 1, 0, 0)} }}
], 
        allowDiskUse: true})

这里allowDiskUse将有助于找出超过16MB的数据,正如@Frederick所说的,mongo 2.6至少是mongo文档中的链接,供进一步参考,该链接与Runway命令类似,但与db.collection.aggreagate一起工作。请注意,对于文档限制使用“游标”选项,对于排序限制使用“allowDiskUse”选项。

您可以使用
aggregateCursors(集合名称,$pipeLine,[“useCursor”=>true])


事实上,我已经启用了
allowDiskUse
选项,但它仍然不起作用。请您提供一个小Java示例或至少一个源代码?非常抱歉-我不熟悉Java mongo API,无论如何,感谢您的快速响应。我找到了一个解决方案(使用spring数据和mongoDB):
List pipeline=new ArrayList();DBObject someMatchCriteria=新的BasicDBObject();someMatchCriteria.put(“参数”、“值”);DBObject out=新的BasicDBObject();out.put(“$out”,“outCollectionName”);添加(新的BasicDBObject(“$match”,someMatchCriteria));管道。添加(输出);mongoOperations.getCollection(“inCollectionName”).aggregate(管道)
db.patients.runCommand('aggregate', 
        {pipeline: [
    {"$project": {"birthday":1, "id":1}},
    {"$match": { "birthday":{"$gte":datetime.datetime(1987, 1, 1, 0, 0)} }}
], 
        allowDiskUse: true})
pipe = [
    {"$match": { "birthday":{"$gte":datetime.datetime(1987, 1, 1, 0, 0)} }}
    ]
res =db.patients.aggregateCursor(collection_name, pipe, ["useCursor" => true]);
        
$ret = [];

foreach ($taskList as $task){
  array_push($ret, $task);
}
        
return $ret;