代码之家  ›  专栏  ›  技术社区  ›  atlas_scoffed

Java MongoDB 3.6带游标选项的聚合[duplicate]

  •  0
  • atlas_scoffed  · 技术社区  · 7 年前

    这是我希望使用Java mongodb驱动程序运行的聚合。

    db.mycollection.aggregate(
       [
         {
           $group:
             {            
               _id: {  status: "$status" },
               totalAmount: { $sum: "$filesize" }
             }
         }
       ]
    )
    

    命令在Shell中运行良好,与Mongo3.6相对应。

    “errmsg”:“需要使用‘游标’选项,但使用 “解释论点”

    我无法从Java文档中找出如何指定“游标”选项,或文档中指定的任何选项:

    https://docs.mongodb.com/manual/reference/method/db.collection.aggregate/#db.collection.aggregate

    在Java中,aggregate命令将只接受以下参数:

    • 聚合。。。

    加积选项: -输出模式 -maxTime

    1 回复  |  直到 7 年前
        1
  •  3
  •   atlas_scoffed    7 年前

    写下这个问题,我得出了以下解决方案:

    你必须把它设置好 光标 ,例如:

    AggregationOptions aggregationOptions = AggregationOptions.builder().outputMode(AggregationOptions.OutputMode.CURSOR).build();
    
    Iterator<DBObject> cursor = getCollection().aggregate(pipeline, aggregationOptions);
    

    • 内联。

    内联返回一个错误。。。

    这似乎是有道理的,允许 作为一个DBObject文档。或者,为采用DBObject的AggregationOptions创建一个构造函数。