Spring Data MongoDB-以及其他集合的聚合 [英] Spring Data MongoDB - Aggregation with other collection as well

查看:89
本文介绍了Spring Data MongoDB-以及其他集合的聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究 Spring Boot 2.1.3.3.RELEASE和Spring Data MongoDB .由于关键的要求,我不得不假设员工了解多种技术,但主要语言是任何人.

因此,我决定将技术集合与在员工集合中与员工和技术相关的方式分开.

{
    "_id" : ObjectId("5ec65750fdcd4e960f4b2f24"),
    "technologyCd" : "AB",
    "technologyName" : "My ABC",
    "ltechnologyNativeName" : "XY",
    "status" : "A"
}

所以,我完成了如下所示的关联-

注意:多个技术可以与一名员工关联

一个员工可以与多种技术相关联

员工只能拥有一项主要技术

{
    "_id" : ObjectId("5ec507c72d8c2136245d35ce"),
    "firstName" : "John",
    "lastName" : "Doe",
    "email" : "j.d@gmail.com",
    .......
    .......
    .......
    "employeeTechnologyRefs" : [ 
        {
            "technologyCd" : "AB",
            "primaryTechnologySw" : "Y",
            "Active" : "A"
        }, 
        {
            "technologyCd" : "AB",
            "primaryTechnologySw" : "N",
            "Active" : "A"
        }, 
        {
            "technologyCd" : "PR",
            "primaryTechnologySw" : "N",
            "Active" : "A"
        }, 
        {
            "technologyCd" : "PR",
            "primaryTechnologySw" : "N",
            "Active" : "A"
        }
    ],
    "countryPhoneCodes" : [ 
        "+352"
    ],
    ....
    ...
}

我在下面使用查询,如何查询技术文档以获取结果并将其映射并创建最终对象?

Criteria criteria = new Criteria();
criteria.andOperator(
        StringUtils.isNotBlank(firstName) ? Criteria.where("firstName").is(firstName.toUpperCase())
                : Criteria.where(""),
        StringUtils.isNotBlank(lastName) ? Criteria.where("lastName").is(lastName.toUpperCase())
                : Criteria.where(""),
        StringUtils.isNotBlank(email) ? Criteria.where("email").is(email.toUpperCase())
                : Criteria.where(""),
        StringUtils.isNotBlank(technologyCd) ? Criteria.where("employeeTechnologyRefs.technologyCd").is(technologyCd.toUpperCase())
                : Criteria.where(""));

MatchOperation matchStage = Aggregation.match(criteria);

GroupOperation groupOp = Aggregation
        .group("firstName", "lastName", "email","_id")
        .addToSet("employeeTechnologyRefs").as("employeeTechnologyRefs");

ProjectionOperation projectStage = Aggregation.project("employeeTechnologyRefs");

Aggregation aggregation = Aggregation.newAggregation(matchStage, groupOp, projectStage);

AggregationResults<CustomObject> results = mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(Employee.class), CustomObject.class);
System.out.println(results);

结果应如下图所示

{
    "_id" : ObjectId("5ec507c72d8c2136245d35ce"),
    "firstName" : 442,
    "lastName" : "LU",
    "email" : "LUX",
    .......
    .......
    .......
    "employeeTechnologyRefs" : [ 
        {
            "technologyCd" : "AB",
            "technologyName" : "My ABC",
            "ltechnologyNativeName" : "XY",
            "primaryTechnologySw" : "Y",
            "Active" : "A"
        }, 
        {
            "technologyCd" : "AB",
            "technologyCd" : "AB",
            "technologyName" : "My ABC",
            "ltechnologyNativeName" : "XY",
            "primaryTechnologySw" : "Y",
            "Active" : "A"
        }, 
        {
            "technologyCd" : "PR",
            "technologyCd" : "AB",
            "technologyName" : "My ABC",
            "ltechnologyNativeName" : "XY",
            "primaryTechnologySw" : "Y",
            "Active" : "A"
        }, 
        {
            "technologyCd" : "PR",
            "technologyCd" : "AB",
            "technologyName" : "My ABC",
            "ltechnologyNativeName" : "XY",
            "primaryTechnologySw" : "Y",
            "Active" : "A"
        }
    ],
    ....
    
}

解决方案

如果您在代码中使用以下查找操作,则应该能够按预期获得答案,并且您无需在代码中进行分组操作./p>

编辑答案:这是整个代码的外观.还有一件事,您不需要投影,并且如果您只需要尝试投影特定字段,并且作为查找操作的一部分,请不要使用与集合中相同的字段,否则它将覆盖员工集合中的现有数据.

    Criteria criteria = new Criteria();
    criteria.andOperator(
            StringUtils.isNotBlank(firstName) ? Criteria.where("firstName").is(firstName.toUpperCase())
                    : Criteria.where(""),
            StringUtils.isNotBlank(lastName) ? Criteria.where("lastName").is(lastName.toUpperCase())
                    : Criteria.where(""),
            StringUtils.isNotBlank(email) ? Criteria.where("email").is(email.toUpperCase())
                    : Criteria.where(""),
            StringUtils.isNotBlank(technologyCd) ? Criteria.where("employeeTechnologyRefs.technologyCd").is(technologyCd.toUpperCase())
                    : Criteria.where(""));

    MatchOperation matchStage = Aggregation.match(criteria);

    /*GroupOperation groupOp = Aggregation
            .group("firstName", "lastName", "email","_id")
            .addToSet("employeeTechnologyRefs").as("employeeTechnologyRefs");
            */

    LookupOperation lookupOperation = LookupOperation.newLookup().
                                     from("technology_collection_name").
                                     localField("employeeTechnologyRefs.technologyCd").
                                     foreignField("technologyCd").
                                     as("employeeTechnologyRefsOtherName");

   /* ProjectionOperation projectStage = Aggregation.project("employeeTechnologyRefs");
  */  
// And if you want to project specific field from employee array you can use something like.
ProjectionOperation projectStage = Aggregation.project("employeeTechnologyRefs.fieldName")
    Aggregation aggregation = Aggregation.newAggregation(matchStage, lookupOperation, projectStage);

    AggregationResults<CustomObject> results = mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(Employee.class), CustomObject.class);
    System.out.println(results);

I'm working on Spring Boot v.2.1.3.RELEASE and Spring Data MongoDB. Due to critical requirement, I had to model like below assuming Employee knows multiple technologies, but primary language would be anyone.

So, I decided to keep technology collection separate and somehow related Employee and technology in the Employee Collection.

{
    "_id" : ObjectId("5ec65750fdcd4e960f4b2f24"),
    "technologyCd" : "AB",
    "technologyName" : "My ABC",
    "ltechnologyNativeName" : "XY",
    "status" : "A"
}

So, I've done association like below -

Note: Multiple Technologies can be associated with one Employee

One Employee can be associated with multiple technologies

Employee can have one and only one Primary Technology

{
    "_id" : ObjectId("5ec507c72d8c2136245d35ce"),
    "firstName" : "John",
    "lastName" : "Doe",
    "email" : "j.d@gmail.com",
    .......
    .......
    .......
    "employeeTechnologyRefs" : [ 
        {
            "technologyCd" : "AB",
            "primaryTechnologySw" : "Y",
            "Active" : "A"
        }, 
        {
            "technologyCd" : "AB",
            "primaryTechnologySw" : "N",
            "Active" : "A"
        }, 
        {
            "technologyCd" : "PR",
            "primaryTechnologySw" : "N",
            "Active" : "A"
        }, 
        {
            "technologyCd" : "PR",
            "primaryTechnologySw" : "N",
            "Active" : "A"
        }
    ],
    "countryPhoneCodes" : [ 
        "+352"
    ],
    ....
    ...
}

I used below query, how to query over the Technology documents to get the result and map it and create final object?

Criteria criteria = new Criteria();
criteria.andOperator(
        StringUtils.isNotBlank(firstName) ? Criteria.where("firstName").is(firstName.toUpperCase())
                : Criteria.where(""),
        StringUtils.isNotBlank(lastName) ? Criteria.where("lastName").is(lastName.toUpperCase())
                : Criteria.where(""),
        StringUtils.isNotBlank(email) ? Criteria.where("email").is(email.toUpperCase())
                : Criteria.where(""),
        StringUtils.isNotBlank(technologyCd) ? Criteria.where("employeeTechnologyRefs.technologyCd").is(technologyCd.toUpperCase())
                : Criteria.where(""));

MatchOperation matchStage = Aggregation.match(criteria);

GroupOperation groupOp = Aggregation
        .group("firstName", "lastName", "email","_id")
        .addToSet("employeeTechnologyRefs").as("employeeTechnologyRefs");

ProjectionOperation projectStage = Aggregation.project("employeeTechnologyRefs");

Aggregation aggregation = Aggregation.newAggregation(matchStage, groupOp, projectStage);

AggregationResults<CustomObject> results = mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(Employee.class), CustomObject.class);
System.out.println(results);

Result should look like below

{
    "_id" : ObjectId("5ec507c72d8c2136245d35ce"),
    "firstName" : 442,
    "lastName" : "LU",
    "email" : "LUX",
    .......
    .......
    .......
    "employeeTechnologyRefs" : [ 
        {
            "technologyCd" : "AB",
            "technologyName" : "My ABC",
            "ltechnologyNativeName" : "XY",
            "primaryTechnologySw" : "Y",
            "Active" : "A"
        }, 
        {
            "technologyCd" : "AB",
            "technologyCd" : "AB",
            "technologyName" : "My ABC",
            "ltechnologyNativeName" : "XY",
            "primaryTechnologySw" : "Y",
            "Active" : "A"
        }, 
        {
            "technologyCd" : "PR",
            "technologyCd" : "AB",
            "technologyName" : "My ABC",
            "ltechnologyNativeName" : "XY",
            "primaryTechnologySw" : "Y",
            "Active" : "A"
        }, 
        {
            "technologyCd" : "PR",
            "technologyCd" : "AB",
            "technologyName" : "My ABC",
            "ltechnologyNativeName" : "XY",
            "primaryTechnologySw" : "Y",
            "Active" : "A"
        }
    ],
    ....
    
}

解决方案

If you use below lookup operation in your code, you should able to get the answer as expected, and you don't need group operation in your code.

Edited Answer: This is how whole code should look like. One more thing, you don't need projection and if you need try projecting specific field only, and as part of the lookup operation do not use the same field as it is in your collection, otherwise it will override existing data from employee collection.

    Criteria criteria = new Criteria();
    criteria.andOperator(
            StringUtils.isNotBlank(firstName) ? Criteria.where("firstName").is(firstName.toUpperCase())
                    : Criteria.where(""),
            StringUtils.isNotBlank(lastName) ? Criteria.where("lastName").is(lastName.toUpperCase())
                    : Criteria.where(""),
            StringUtils.isNotBlank(email) ? Criteria.where("email").is(email.toUpperCase())
                    : Criteria.where(""),
            StringUtils.isNotBlank(technologyCd) ? Criteria.where("employeeTechnologyRefs.technologyCd").is(technologyCd.toUpperCase())
                    : Criteria.where(""));

    MatchOperation matchStage = Aggregation.match(criteria);

    /*GroupOperation groupOp = Aggregation
            .group("firstName", "lastName", "email","_id")
            .addToSet("employeeTechnologyRefs").as("employeeTechnologyRefs");
            */

    LookupOperation lookupOperation = LookupOperation.newLookup().
                                     from("technology_collection_name").
                                     localField("employeeTechnologyRefs.technologyCd").
                                     foreignField("technologyCd").
                                     as("employeeTechnologyRefsOtherName");

   /* ProjectionOperation projectStage = Aggregation.project("employeeTechnologyRefs");
  */  
// And if you want to project specific field from employee array you can use something like.
ProjectionOperation projectStage = Aggregation.project("employeeTechnologyRefs.fieldName")
    Aggregation aggregation = Aggregation.newAggregation(matchStage, lookupOperation, projectStage);

    AggregationResults<CustomObject> results = mongoTemplate.aggregate(aggregation, mongoTemplate.getCollectionName(Employee.class), CustomObject.class);
    System.out.println(results);

这篇关于Spring Data MongoDB-以及其他集合的聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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