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

使用WSO2EI和MongoDB进行自定义聚合

  •  1
  • vod0029  · 技术社区  · 7 年前

    我正试图在WSO2DSS(在WSO2EI中)中实现Mongo数据服务的聚合操作,因为只有CRUD和Count这样的基本操作才受到开箱即用的支持 here . 所以我克隆了 WSO2EI code version 4.4.10 (我们的团队碰巧正在使用此版本),我已经成功添加了一些自己的功能。然而,每当我尝试使用 org.jongo.MongoCollection.aggregate() 函数,我得到错误 Error in MongoQuery.runQuery: Command failed with error 9: 'The 'cursor' option is required, except for aggregate with the explain argument' . 我查看了所有关于这个问题的帖子,并尝试使用不同版本的Mongo驱动程序(3.4、3.6..),也更改了很多实际语法,但无论我尝试什么,如果我使用 .aggregate() 作用我也应该使用 org.jongo.MongoCollection.aggregate(String pipelineOperator) com.mongodb.async.client.aggregate(java.util.List<? extends Bson> pipeline) 如Mongo Java 3.6文档所述 here . 我使用的示例代码:

    private Iterator<MongoTestClass> doAggregate1(MongoCollection collection, String opQuery, Object[] parameters) throws DataServiceFault {
    
            return collection.aggregate("{$match:{componentType:'Endpoint'}}")
                    .as(MongoTestClass.class).iterator();
    
        }
    

    哪里 componentType 是我的MongoDB集合文档中的现有字段,并且 'Endpoint' 就是它的价值。我的实际synatax错了吗?或者还有其他问题吗?如何添加 'cursor' 这样错误就消失了?

    我不知道这是怎么回事。。。非常感谢您的帮助。

    1 回复  |  直到 7 年前
        1
  •  0
  •   s7vr    7 年前

    从文档中。

    MongoDB 3.4不赞成使用不带光标的聚合命令 选项,除非管道包含explain选项。什么时候 使用aggregate命令内联返回聚合结果, 使用默认的批大小游标指定游标选项:{}或 在光标选项光标中指定批次大小:{批次大小: }.

    你可以通过 batchSize 具有 AggregationOptions Jongo骨料法。

    AggregationOptions options = AggregationOptions.builder().options.
         batchSize(100).
         outputMode(AggregationOptions.OutputMode.CURSOR).build();
    
    collection.aggregate("{$match:{componentType:'Endpoint'}}").options(options).as(MongoTestClass.class).iterator();
    

    使用默认批量大小

    AggregationOptions options = AggregationOptions.builder().options.
         outputMode(AggregationOptions.OutputMode.CURSOR).build();
    

    AggregationOptions options = AggregationOptions.builder().build();
    
    collection.aggregate("{$match:{componentType:'Endpoint'}}").options(options).as(MongoTestClass.class).iterator();