用于聚合$ map的SpringData mongoDB API

时间:2017-05-13 11:16:07

标签: mongodb spring-data-mongodb

以下是我的Native MongoDB查询,下面是SpringData Mongo API的等价物。我在SpringData Mongo API中使用$ project中的$ map时感到非常震惊。感谢您完成我转换为API的帮助

db.users.aggregate([    
    { $match: {$and : [{userType:"200"} },    
     { $unwind: "$userOrgMap" },
    {
      $lookup:
        {
          from: "users",
          localField: "userOrgMap.createdbyuser",
          foreignField: "_id",
          as: "created_by"
        }
   },
   {$project:{
       _id:"$_id",
       login:"$login",
       firstName:"$firstName",
       lastName:"$lastName",
       email:"$email",
       deactivateFlag:"$deactivateFlag",
       createdOn:"$createdOn",
       createdBy:{
        "$map": { 
                    "input": "$created_by", 
                    "as": "u", 
                      "in": { 
                          "name": { "$concat" : [ "$$u.firstName", " ", "$$u.lastName" ] }, 
                      } 
                }
    }
      }
    },
    { $sort : { createdBy : 1} }

])

Spring Query

 Aggregation aggregation = newAggregation(
            Aggregation.match(Criteria.where("userType").is(userType)),
            Aggregation.unwind("userOrgMap"),
            Aggregation.lookup("users", "userOrgMap.createdbyuser", "_id", "created_by"),
            Aggregation.project("userId","login","firstName","lastName","email","deactivateFlag","createdOn")
        );

2 个答案:

答案 0 :(得分:2)

您可以尝试以下$map聚合。

  project().and(mapItemsOf("created_by").
                            as("u").
                            andApply(new AggregationExpression() {
                                @Override
                                public DBObject toDbObject(AggregationOperationContext aggregationOperationContext) {
                                    return new BasicDBObject("name", StringOperators.valueOf("u.firstName").concat(" ").concatValueOf("u.lastName").toDbObject(aggregationOperationContext));
                                }
                            }))
             .as("createdBy");

使用$let表达式

 project().and(VariableOperators.Let.define(VariableOperators.Let.ExpressionVariable.newVariable("u").forExpression(ArrayOperators.arrayOf("created_by").elementAt(0))).
                            andApply(new AggregationExpression() {
                                @Override
                                public DBObject toDbObject(AggregationOperationContext aggregationOperationContext) {
                                    return new BasicDBObject("name", StringOperators.valueOf("u.firstName").concat(" ").concatValueOf("u.lastName").toDbObject(aggregationOperationContext));
                                }
                            }))
            .as("createdBy");

使用lambda和静态导入。

import static org.springframework.data.mongodb.core.aggregation.ArrayOperators.arrayOf;
import static org.springframework.data.mongodb.core.aggregation.StringOperators.valueOf;
import static org.springframework.data.mongodb.core.aggregation.VariableOperators.Let.ExpressionVariable.*;
import static org.springframework.data.mongodb.core.aggregation.VariableOperators.Let.define;
import static org.springframework.data.mongodb.core.aggregation.VariableOperators.mapItemsOf;

project().and(mapItemsOf("created_by").
                as("u").
                andApply(aggregationOperationContext -> new BasicDBObject("name", valueOf("u.firstName").concat(" ").concatValueOf("u.lastName").toDbObject(aggregationOperationContext))))
                .as("createdBy");

    project().and(define(newVariable("u").forExpression(arrayOf("created_by").elementAt(0))).
            andApply(aggregationOperationContext -> new BasicDBObject("name", valueOf("u.firstName").concat(" ").concatValueOf("u.lastName").toDbObject(aggregationOperationContext))))
            .as("createdBy");

答案 1 :(得分:0)

这是我能够做到的另一种方法。

Aggregation.project("userId","login","firstName","lastName","email","deactivateFlag","createdOn").and(VariableOperators.Map.itemsOf("created_by").as("u").andApply(StringOperators.Concat.valueOf("$$u.firstName").concat(" ").concatValueOf("$$u.lastName"))).as("createdBy");