更新时间:2023-01-16 07:43:38
需要细微调整才能完成工作,以下是查询:
A minor tweak does the job, below is the query :
user1 = list(community.aggregate([{
"$match": { "_id": ObjectId("5dc1f30abcafe70001bd0075") }
},
{
"$lookup": {
"from": "profile",
"localField": "user_id",
"foreignField": "_id",
"as": "details"
}
}, { "$unwind": "$details" }, { "$match": { "details.username": /eesh/i } }, {
"$project": {
"_id": 0,
"username": "$details.username",
"name": "$details.name"
}
}]))
请根据您的要求动态传递/eesh/i
.换句话说,我尝试在$lookup
本身中添加此过滤器{ "details.username": /eesh/i }
-这样我们就可以避免遵循$unwind
& $match
阶段,但是由于要检查两个过滤器-由于$match
的$expr
中使用regex
的限制很少,因此此查询为&甚至更容易做到.
Please pass /eesh/i
dynamically as per your requirement. In the other way I've tried to add this filter { "details.username": /eesh/i }
in $lookup
itself - that way we can avoid following $unwind
& $match
stages, But as we've two filters to be checked - due to few limitations on using regex
in $expr
of $match
, this query is the one & even this is easier to do.