sql语句如下:
db.getCollection('user').aggregate([
{
$lookup: { // 左连接
from: "message", // 关联到message表
localField: "thirdId", // user 表关联的字段
foreignField: "fromId", // message 表关联的字段
as: "message"
}
},
{
$match:{
"valid":{$exists:false},
"message":{$ne: {$size: 0}},
"message.msgType" : "text"
}
}
])
对应代码如下:
public List<User> findValid(){
Aggregation agg = Aggregation.newAggregation(
Aggregation.match(Criteria.where("valid").exists(false).and("message.msgType").is("text")),
Aggregation.lookup("message", "thirdId", "fromId", "message")
);
AggregationResults<User> result = mongoTemplate.aggregate(agg,"user", User.class);
List<User> userList = result.getMappedResults();
log.info("userList.size() {}", userList.size());
return userList;
}