使用Java的Mongo 3.2驱动程序的新聚合功能 [英] New aggregation feature with Mongo 3.2 driver, using Java

查看:233
本文介绍了使用Java的Mongo 3.2驱动程序的新聚合功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Mongo 3.2中执行聚合,如此处所述,但在Java中:

I want to perform an aggregation in Mongo 3.2 as explained here, but in Java:

https://docs.mongodb.org/master/reference/operator/aggregation/lookup/#pipe._S_lookup

目前我在java中的查询对象非常简单:

At the moment my query object in java is very simple:

    Document query = new Document();

    query.append("employeId", employeId);

除了通过employeId过滤,我想加入这个集合与公司(employee.company_id) = company.id)

In addition to filtering by employeId, I would like to join this collection with company (where employee.company_id = company.id)

我怎样才能用Java做到这一点?好像我找不到这个新的Mongo功能的文档。

How can I do that in Java? It seems like I cannot find documentation of this new Mongo feature.

编辑

员工收集示例:

{
    "id" : 1,
    "name" : "John",
    "lastName" : "Moore",
    "age" : 44,
    "companyId": 10 
}

公司集合示例:

{
    "id" : 10,
    "companyName" : "Microsoft",
    "numEmployee" : 100
}

预期产出示例

{
    "id" : 1,
    "name" : "John",
    "lastName" : "Moore",
    "companyId" : 10,
    "companyName" : "Microsoft"
}


推荐答案

运行以下聚合管道应该可以得到所需的结果

Running the following aggregation pipeline should give you the needed result

pipeline = [
    {
        "$match": {
            "_id": employeeId
        }
    },
    {
        "$lookup": {
            "from": "company", 
            "localField": "companyId",
            "foreignField": "_id",
            "as": "company"
        }
    },
    {
        "$project": {
            "name": 1,
            "lastName": 1,
            "companyId": 1,
            "companyName": "$company.companyName"
        }
    }
];
db.employee.aggregate(pipeline);






Java测试实施

public class JavaAggregation {
    public static void main(String args[]) throws UnknownHostException {

        MongoClient mongo = new MongoClient();
        DB db = mongo.getDB("test");

        DBCollection coll = db.getCollection("employee");

        // create the pipeline operations, first with the $match
        DBObject match = new BasicDBObject("$match",
            new BasicDBObject("_id", employeeId)
        );

        // build the $lookup operations
        DBObject lookupFields = new BasicDBObject("from", "company");
        lookupFields.put("localField", "companyId");
        lookupFields.put("foreignField", "_id");
        lookupFields.put("as", "company");      
        DBObject lookup = new BasicDBObject("$lookup", lookupFields);

        // build the $project operations
        DBObject projectFields = new BasicDBObject("name", 1);
        projectFields.put("lastName", 1);
        projectFields.put("companyId", 1);
        projectFields.put("companyName", "$company.companyName");       
        DBObject project = new BasicDBObject("$project", projectFields);

        List<DBObject> pipeline = Arrays.asList(match, lookup, project);

        AggregationOutput output = coll.aggregate(pipeline);

        for (DBObject result : output.results()) {
            System.out.println(result);
        }
    }
}

这篇关于使用Java的Mongo 3.2驱动程序的新聚合功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆