java - 如何使用 MongoDB 3.0 java 驱动程序执行聚合

标签 java mongodb

我正在尝试使用 MongoDB java 驱动程序 (3.0) 执行简单的聚合操作。需要有关如何使用 JAVA API 编写以下 mongo shell 查询的帮助。

db.coll.aggregate([
    { $project: {
        email_id: { $ifNull: [ "$email_id", " " ] },
        phone_num: { $ifNull: [ "$phone_num","NA" ] },
        id : 1,
        firstname :1,
        lastname :1,
        status : 1
        }
    },
    { $match: { status: "true"} },
    { "$group": {
        "_id": "$id",
        "details": { "$push": {
            "$concat": [ "$firstname", " ", "$lastname", " | ", "$email_id" , " | ", "$phone_num" ]
        }}
    }}
])

最佳答案

这是您的 shell 查询的 java 驱动程序查询

MongoClient mongo = new MongoClient();    
MongoDatabase database = mongo.getDatabase("your_database");
        Document emailIdDoc = new Document("$ifNull",Arrays.asList("$email_id", " "));
        Document phoneNumDoc = new Document("$ifNull", Arrays.asList("$phone_num", "NA")); 
        Document projectDoc = new Document("$project" , new Document("email_id", emailIdDoc).append("phone_num", phoneNumDoc).append("id" , 1).append("firstname" , 1).append("lastname", 1).append("status", 1));
        Document matchDoc = new Document("$match", new BasicDBObject("status", "true"));
        Document groupDoc = new Document("$group", new Document("_id", "$id").append("details", new Document("$push", concat)));
        AggregateIterable<Document> aggregationResult = db.getCollection("your_collection").aggregate(asList(projectDoc, matchDoc, groupDoc));

使用弃用的方法

MongoClient mongo = new MongoClient();
    DB db = mongo.getDB("your_database");
    BasicDBObject emailId = new BasicDBObject("$ifNull",Arrays.asList("$email_id", " "));
    BasicDBObject phoneNum = new BasicDBObject("$ifNull", Arrays.asList("$phone_num", "NA"));
    DBObject project = new BasicDBObject("$project" , new BasicDBObject("email_id", emailId).append("phone_num", phoneNum).append("id" , 1).append("firstname" , 1).append("lastname", 1).append("status", 1));
    DBObject match = new BasicDBObject("$match", new BasicDBObject("status", "true")); 
    DBObject concat = new BasicDBObject("$concat", Arrays.asList( "$firstname", " ", "$lastname", " | ", "$email_id" , " | ", "$phone_num"));
    Document concatDoc = new Document("$concat", Arrays.asList( "$firstname", " ", "$lastname", " | ", "$email_id" , " | ", "$phone_num"));
    DBObject group = new BasicDBObject("$group", new BasicDBObject("_id", "$id").append("details", new BasicDBObject("$push", concat)));

    AggregationOutput iterable = db.getCollection("your_collection").aggregate(project, match, group);

关于java - 如何使用 MongoDB 3.0 java 驱动程序执行聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34492054/

相关文章:

c# - Java 中 TimeZone.getDefault().getID() 在 C# 中的等效项是什么?

java - 使用 MouseMotionListener 通过 heatMap 进行双矩阵坐标

java - 如何使用 Java API 将文件从一个 HDFS 文件夹复制到另一个 HDFS 文件夹?

ruby-on-rails - Mongo::Error::UnsupportedFeatures(位于 (localhost:27017) 的服务器报告有线版本 (2),但此版本的 Ruby 驱动程序至少需要 (6)。)

node.js - 网址表示

java - 错误的 hibernate 列类型 : Found: undefined

java - 如何封装未经检查的强制转换以仅允许未经检查的泛型类型转换?

嵌入文档数组中的 Mongodb 排序数组

javascript - 如何在node.js中将数据从 Controller 传递到路由器?

json - 查询嵌套 JSON 文档中的多个元素