2017-05-13 63 views
0

以下是我的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} } 

]) 

春查詢

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") 
     ); 
+0

[過濾器陣列中的子文檔陣列字段中Spring框架(的可能的複製http://stackoverflow.com/questions/41847249/filter-array-in-sub-document-array-field-in -spring-framework) – Veeram

+0

感謝您的信息。但我需要更多的幫助來找出API。我的要求是不同的。 Aggregation.project(「userId」,「login」,「firstName」,「lastName」,「email」,「deactivateFlag」,「createdOn」) \t \t .and(mapItemsOf(「created_by」)。as(「u」 )。 \t \t \t \t ??? \t \t))。如( 「createdBy」) – karmaker

回答

0

你可以試試下面$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"); 
+0

感謝您快速reply.But我得到 「是」 爲未定義 – karmaker

+0

Np個。更新答案以包含您使用地圖的'name'別名。我在api中看不到在嵌套表達式中提供別名的任何內容。所以我利用'BasicDBObject'來創建表達式。你可以用'$ let'操作符通過'$ arrayElemAt'和'$ concat'完成這個操作。 – Veeram

+0

我的錯誤。有效。謝謝 – karmaker