Java 从子阵列连接多个记录

Java 从子阵列连接多个记录,java,mongodb,Java,Mongodb,我有一个leads数组的员工记录,其中只包含id,本质上是他领导的其他员工 我想输出结果,将每个潜在客户与员工集合中的名称连接起来 { "_id" : ObjectId("5d4dc8635dd32dbcba4ae0ba"), "name" : "John" } { "_id" : ObjectId("5d4dc8635dd32dbcba4ae0bb"), "name" : "Jane" } { "_id" : ObjectId("5d4dc8635dd3

我有一个leads数组的员工记录,其中只包含id,本质上是他领导的其他员工

我想输出结果,将每个潜在客户与员工集合中的名称连接起来

{
    "_id" : ObjectId("5d4dc8635dd32dbcba4ae0ba"),
    "name" : "John"
}
{
    "_id" : ObjectId("5d4dc8635dd32dbcba4ae0bb"),
    "name" : "Jane"
}
{
    "_id" : ObjectId("5d4dc8635dd32dbcba4ae0b4"),
    "name" : "Richard"
}
{
    "_id" : ObjectId("5d55ac30e533bc76e4581923"),
    "employee_id" : ObjectId("5d4dc8635dd32dbcba4ae0c5"),
    "leads" : [ 
        ObjectId("5d4dc8635dd32dbcba4ae0ba"), 
        ObjectId("5d4dc8635dd32dbcba4ae0bb")
    ]
}
员工收集

{
    "_id" : ObjectId("5d4dc8635dd32dbcba4ae0ba"),
    "name" : "John"
}
{
    "_id" : ObjectId("5d4dc8635dd32dbcba4ae0bb"),
    "name" : "Jane"
}
{
    "_id" : ObjectId("5d4dc8635dd32dbcba4ae0b4"),
    "name" : "Richard"
}
{
    "_id" : ObjectId("5d55ac30e533bc76e4581923"),
    "employee_id" : ObjectId("5d4dc8635dd32dbcba4ae0c5"),
    "leads" : [ 
        ObjectId("5d4dc8635dd32dbcba4ae0ba"), 
        ObjectId("5d4dc8635dd32dbcba4ae0bb")
    ]
}
员工领导收集

{
    "_id" : ObjectId("5d4dc8635dd32dbcba4ae0ba"),
    "name" : "John"
}
{
    "_id" : ObjectId("5d4dc8635dd32dbcba4ae0bb"),
    "name" : "Jane"
}
{
    "_id" : ObjectId("5d4dc8635dd32dbcba4ae0b4"),
    "name" : "Richard"
}
{
    "_id" : ObjectId("5d55ac30e533bc76e4581923"),
    "employee_id" : ObjectId("5d4dc8635dd32dbcba4ae0c5"),
    "leads" : [ 
        ObjectId("5d4dc8635dd32dbcba4ae0ba"), 
        ObjectId("5d4dc8635dd32dbcba4ae0bb")
    ]
}
预期产量

{
    "_id" : ObjectId("5d4dc8635dd32dbcba4ae0c3"),
    "leads" : [ 
        {
            "_id" : ObjectId("5d4dc8635dd32dbcba4ae0ba"),
            "name" : "John"
        }, 
        {
            "_id" : ObjectId("5d4dc8635dd32dbcba4ae0bb"),
            "name" : "Jane"
        }
    ]
}
尝试:

Document match = new Document("$match", new BasicDBObject("_id", new ObjectId("5d55ac30e533bc76e4581923")));

Document lookup = new Document("$lookup", new BasicDBObject("from", "employee"))
        .append("localField", "leads")
        .append("foreignField", "_id")
        .append("as", "leads");

// Document unwind = new Document("$unwind", "$leads");

Document project = new Document("$project", new BasicDBObject("name", "$lead.name"));

Document document = database.getCollection("employee_lead")
        .aggregate(Arrays.asList(match, lookup, unwind, project))
        .first();

// TODO: iterate through the lead array and display it

问题是,是有一个join语句,还是我必须对数据库进行多个调用(天真的方式)?

您不必对数据库进行多个调用,在聚合框架中会完全按照您的要求执行

请尝试以下查询:

db.getCollection("employee_leads").aggregate([
{
        $match : {
            "_id" : new ObjectId("5d55ac30e533bc76e4581923") // This is in case you want to filter anything. 
        }
},
{
        $lookup : {
            "from": "employee",
            "localField": "leads",
            "foreignField": "_id",
            "as": "leads"
        }
}])
上述查询的Java等效代码:

示例1

List<Bson> aggregates = Arrays.asList(
                Aggregates.match(Filters.eq("_id", new ObjectId("5d55ac30e533bc76e4581923"))),
                Aggregates.lookup("employee", "leads", "_id", "leads"));
        AggregateIterable<Document> iterable = this.collection.aggregate(aggregates);
List aggregates=Arrays.asList(
聚合.match(Filters.eq(“_id”,新ObjectId)(“5d55ac30e533bc76e4581923”),
聚合。查找(“员工”、“潜在客户”、“用户id”、“潜在客户”);
AggregateIterable=this.collection.aggregate(聚合);
示例2

List<Document> aggregates = Arrays.asList(
        new Document("$match", new Document("_id", new ObjectId("5d55ac30e533bc76e4581923"))),
        new Document("$lookup", new Document("from", "employee")
        .append("localField", "leads")
        .append("foreignField", "_id")
        .append("as", "leads")));

AggregateIterable<Document> iterable = collection.aggregate(aggregates);

for (Document row : iterable) {
    System.out.println(row.toJson());
}
List aggregates=Arrays.asList(
新文档($match),新文档('u id',新ObjectId(“5d55ac30e533bc76e4581923”),
新文档($lookup),新文档(“from”,“employee”)
.append(“localField”、“leads”)
.append(“foreignField”,“_id”)
.附加(“作为”、“线索”);
AggregateIterable=collection.aggregate(聚合);
用于(文档行:iterable){
System.out.println(row.toJson());
}

简单而甜蜜@Sharma女士,我将尝试将这段代码翻译成Java,根据您以前的回答,$unwind对我来说是一个新概念,在尝试使用Java时,我会有点担心。如果您能用一些Java代码支持这段回答,我将不胜感激。@AppDeveloper确定。@AppDeveloper检查: