以下是我的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")
);
[過濾器陣列中的子文檔陣列字段中Spring框架(的可能的複製http://stackoverflow.com/questions/41847249/filter-array-in-sub-document-array-field-in -spring-framework) – Veeram
感謝您的信息。但我需要更多的幫助來找出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