java - 如何使用文档和 mongo 聚合将 mongo shell 代码转换为 java 代码

标签 java mongodb mongodb-query aggregation-framework

如何将这段代码转换为java?如何在java中使用聚合框架?谁能帮帮我吗...

我的java代码:

Document update = new Document("$project",new Document("TOTAL_EMPLOYEE_SALARY",new Document("$sum","$employees.EMP_SALARY")));
 AggregationOutput output = coll.aggregate(update); // throwing some error in eclipse

我的 mongo shell 代码:

db.collection.aggregate([
    { "$project": {
        "TOTAL_EMPLOYEE_SALARY": {
           "$sum": "$employees.EMP_SALARY"
        }
    }}
])

最佳答案

所以使用列表:

List<Document> pipeline = Arrays.<Document>asList(
    new Document("$project",
        new Document("TOTAL_EMPLOYEE_SALARY",new Document("$sum","$employees.EMP_SALARY"))
    )
);

AggregationOutput output = coll.aggregate(pipeline);

关于java - 如何使用文档和 mongo 聚合将 mongo shell 代码转换为 java 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36787731/

相关文章:

java - mvn 依赖导致 htmlunit 出现 NoSuchMethod 错误

node.js - 如何创建 sails.js 框架以便我可以快速构建具有共同目标的新应用程序?

mongodb - mongodb 中的集合扫描是什么?

MongoDB在列表列表中查找值

MongoDB:插入重复键更新

java - 有没有办法删除 CursorLoader() 的选择参数中自动生成的括号?

java - 如何删除字符串中 $ 后面的空格?

c# - 使用 MongoDB C# 驱动程序 2.0 添加元素或添加到数组

javascript - 如何连接到 mongodb 以及如何使用 node.js 程序从 mongodb 数据库打印所有集合的列表

java - 对象映射器从文件读取 JSON 时出错