【发布时间】:2017-01-17 13:49:27
【问题描述】:
如果我用 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 查询中组合这三者?
【问题讨论】:
-
第 1 步:开始编写现代的、显式的 JOIN 语法!
-
我是 MongoDB 新手,不知道现代显式 JOIN 语法是什么……在文档 docs.mongodb.com/manual/reference/operator/aggregation/lookup 中并没有说它已被弃用。
-
Aggregate 实际上是一个管道,在那里匹配你的文档。
-
对不起,我的意思是 SQL JOIN!
-
根据您的个人查询,您只需要在
response($lookup之前)和request($unwind之后,$lookup之后)包含$match阶段) 聚合管道中的集合。
标签: sql mongodb mongodb-query aggregation-framework