0
我們可以在MongoDB中加入兩個具有多個條件的集合嗎?
例
db.Person.aggregate([
{ $lookup : {
from: "Person",
localField: "_id",
localField: 「businessGroup",
foreignField: 「personId",
foreignField: 「businessGroupCode",
as: "person"
} }
])
感謝
更多關於我的查詢
細節我有3個集合COL1,COL2,COL3。我需要加入這些集合使我的輸出應該具有col1和runid爲從col2的
ScenarioName和COL3最新成果(即col2.runId = col3.runId)
**testscenario:**
{
"_id" : ObjectId("57f41cb9319ed34079df8a2d"),
"environment" : "STAGE",
"component" : "test1",
"scenarioName" : "Jira-1234",
"testClass" : "com.test.Test1",
}
**componentrunId:**
{
"_id" : ObjectId("57fc5f56e40a93f2e0ae953c"),
"componentName" : "test1",
"runId" : 415
}
**testResults :**
{
"_id" : ObjectId("5914d0019385b71384e01b2e"),
"_class" : "com.test.TestResults",
"testScenarioId" : ObjectId("5900fbc1aa42d292ecf596ab"),
"runId" : 608,
"runDate" : ISODate("2017-05-11T20:56:19.226Z"),
"status" : "inprogress",
"retryCount" : 0.0
}
/* 2 */
{
"_id" : ObjectId("5915b609637b4a42d362babb"),
"_class" : "com.test.TestResults",
"testScenarioId" : ObjectId("58a4b2028f67f440d08b9845"),
"runId" : 607,
"runDate" : ISODate("2017-05-12T13:18:01.305Z"),
"status" : "passed"
}
/* 3 */
{
"_id" : ObjectId("5915b60c637b4a42d362babc"),
"_class" : "com.test.TestResults",
"testScenarioId" : ObjectId("57ffa1a67ae6ee5093b978cb"),
"runId" : 606,
"runDate" : ISODate("2017-05-12T13:18:04.106Z"),
"status" : "passed"
}
查詢我寫
db.testScenario.aggregate(
[
{
$lookup:
{
from: "componentRunId",
localField: "component",
foreignField: "componentName",
as: "testScenario_docs"
}
},{
$project:{
"_id":"$_id",
"scenarioName" :"$scenarioName",
"runId" : "$testScenario_docs.runId",
"componentName" :"$testScenario_docs.componentName"
}
},
{
$out:"varunTmp"
}
])
db.varunTmp.aggregate([
{$lookup:
{
from: "testResults",
localField: "_id",
//localField: "runId",
foreignField: "testScenarioId",
// foreignField: "runId",
as: "finalResult"
}
},
{
$unwind:"$finalResult"
},
{
$project:{
"runId" : "$finalResult.runId",
"status" : "$finalResult.status",
"scenarioName" :"$scenarioName"
}
}
])
不,你不能。用法與[手冊頁](https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/)中的說明完全相同。雖然您可以執行多個$ lookup階段(這是預期的用法),但我懷疑您正在嘗試執行「分層圖」。並不是說不可能像你可能想要的那樣通過對象「遞歸」。你可能想看看['$ graphLookup'](https://docs.mongodb.com/manual/reference/operator/aggregation/graphLookup/)。但是你的問題缺乏你的結構細節和預期目標。 –
Typo。應該說*「**注意**,這是不可能的..」* –
@NeilLunn:提供了我的問題的更多細節 – nurav