如果我在SQL中编写它,那将是(某种程度上):
SELECT *
FROM request req, response res
WHERE req.suites_test=res.key # join
AND res.version='2.0.b1662.5' # extra conditions
AND req.suites_id='58762c40664df86d2069e2c9'
在MongoDB中,我可以这样做:
# a join between request and response
db.response.aggregate([{$lookup: {from: "request", localField: "key", foreignField: "suites.test", as: "matching"} } ])
# find all requests that match a condition
db.request.find( { "suites.id": ObjectId("58762c40664df86d2069e2c9") } )
# find all responses that match a condition
db.response.find( { "version": "2.0.b1662.5" } )
如何在一个MongoDB查询中组合三个?
答案 0 :(得分:1)
有许多类似的重复,但我找不到完全匹配的副本。所以,我根据OP的要求添加答案。
根据您的个人查询,您只需要包含响应的$match
阶段($lookup
之前)和请求($unwind
之后的$lookup
)汇总管道中的集合。像下面的东西。
db.response.aggregate([{
$match: {
"version": "2.0.b1662.5"
}
}, {
$lookup: {
from: "request",
localField: "key",
foreignField: "suites.test",
as: "matching"
}
}, {
$unwind: "$matching"
}, {
$match: {
"matching.id": ObjectId("58762c40664df86d2069e2c9")
}
}])