下面是我的本机MongoDB查询,下面是SpringData Mongo的等价物。我对在$map中使用$project中的SpringData Mongo很感兴趣。感谢您帮助我完成对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")
);发布于 2017-05-13 11:57:01
您可以在$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");发布于 2019-12-31 20:35:53
这是我能做到的另一种方法。
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");https://stackoverflow.com/questions/43952476
复制相似问题