mongodb shell 查询语句
db.f002.aggregate(
{
$match:{"xsfmc" : "com1"}
}
,
{
$project:{_id:{$toString:"$_id"}}
}
,
{
$lookup:{
from:"f003",
localField:"_id",
foreignField:"originalAssetId",
as:"result"
}
}
,
{
$unwind:"$result"
}
,
{
$match:{"result.state":0,"result.subState":1}
}
,
{
$group: { _id: "$_id", count: { $sum: 1 } }
}
)
Java实现
Aggregation agg = Aggregation.newAggregation(
Aggregation.match(Criteria.where("xsfmc").is("com1"))
,Aggregation.project().and(ConvertOperators.Convert.convertValueOf("_id").to("string")).as("strA")
,Aggregation.lookup("f003","strA","originalAssetId","result")
,Aggregation.unwind("result")
,Aggregation.match(Criteria.where("result.state").is(F003State.available).and("result.subState").is(F003State.sub_rollback))
,Aggregation.group("strA").count().as("intA")
);
最大的困难在于toString的转换