使用选定字段和 mongodb 中的 where 子句连接两个表
Join two tables with selected fields and where clause in mongodb
我有两个合集如下:-
table1
{"_id" : ObjectId("5b9a.."), "item_id" :"1.1", "m_date" : "20130401","ref_id":"12R","sub_item_id":"1.1.1"}
{"_id" : ObjectId("5c37.."), "item_id" :"1.1", "m_date" : "20140401","ref_id":"12R","sub_item_id":"1.1.2"}
{"_id" : ObjectId("123cb.."), "item_id" :"1.2", "m_date" : "20140401","ref_id":"12R","sub_item_id":"1.1.3"}
table2
{"_id" : ObjectId("7cb3.."), "item_id" :"1.1", "m_date" : "20130401","ref_id":"12R","sub_item_id":"1.1.1"}
{"_id" : ObjectId("8f34.."), "item_id" :"1.1", "m_date" : "20140401","ref_id":"13R","sub_item_id":"1.1.2"}
{"_id" : ObjectId("5ec8b.."), "item_id" :"1.2", "m_date" : "20150401","ref_id":"14R","sub_item_id":"1.1.3"}
我想显示来自 table1 :item_id, m_date, sub_item_id
和 table2 : ref_id
的字段,其中 item_id:1。1 这必须在两个表中。所以预期的结果应该显示这个:-
{"item_id" :"1.1", "m_date" : "20130401","sub_item_id":"1.1.1","ref_id":"12R"}
{"item_id" :"1.1", "m_date" : "20140401","sub_item_id":"1.1.2","ref_id":"13R"}
我尝试使用 $lookup
编写以下查询,但发现 0 个 Doc
db.table1.aggregate([
{$project:{
item_id:1,
m_date: 1,
sub_item_id : 1,
ref_id :1
}},
{
$lookup: {
from: 'table2',
localField: 'item_id',
foreignField: 'item_id',
as: 'table2_values'
},
},
{$unwind:'$table2_values'},
{ $group: {
_id: {ref_id: "$table2_values.ref_id", m_date: "$m_date"
,sub_item_id:'$sub_item_id' },
}},
{$project:{_id:0,m_date:'$_id.m_date',ref_id:'$_id.ref_id'
,sub_item_id:'$_id.sub_item_id',item_id:1}},
{
$match: {"table2_values.item_id": "1.1"}
}
])
请帮助我获得上述预期结果
您可以尝试使用 mongodb 3.6
进行以下聚合
db.table1.aggregate([
{ "$match": { "item_id": "1.1" }},
{ "$lookup": {
"from": "table2",
"let": { "item_id": "$item_id", "m_date": "$m_date" },
"pipeline": [
{ "$match": {
"$expr": { "$eq": ["$$item_id", "$item_id" ] },
"$expr": { "$eq": ["$$m_date", "$m_date"] }
}}
],
"as": "table2_values"
}},
{ "$addFields": { "ref_id": { "$arrayElemAt": ["$table2_values.ref_id", 0] }}},
{ "$project": { "_id": 0, "item_id": 1, "ref_id": 1 }}
])
我有两个合集如下:-
table1
{"_id" : ObjectId("5b9a.."), "item_id" :"1.1", "m_date" : "20130401","ref_id":"12R","sub_item_id":"1.1.1"}
{"_id" : ObjectId("5c37.."), "item_id" :"1.1", "m_date" : "20140401","ref_id":"12R","sub_item_id":"1.1.2"}
{"_id" : ObjectId("123cb.."), "item_id" :"1.2", "m_date" : "20140401","ref_id":"12R","sub_item_id":"1.1.3"}
table2
{"_id" : ObjectId("7cb3.."), "item_id" :"1.1", "m_date" : "20130401","ref_id":"12R","sub_item_id":"1.1.1"}
{"_id" : ObjectId("8f34.."), "item_id" :"1.1", "m_date" : "20140401","ref_id":"13R","sub_item_id":"1.1.2"}
{"_id" : ObjectId("5ec8b.."), "item_id" :"1.2", "m_date" : "20150401","ref_id":"14R","sub_item_id":"1.1.3"}
我想显示来自 table1 :item_id, m_date, sub_item_id
和 table2 : ref_id
的字段,其中 item_id:1。1 这必须在两个表中。所以预期的结果应该显示这个:-
{"item_id" :"1.1", "m_date" : "20130401","sub_item_id":"1.1.1","ref_id":"12R"}
{"item_id" :"1.1", "m_date" : "20140401","sub_item_id":"1.1.2","ref_id":"13R"}
我尝试使用 $lookup
编写以下查询,但发现 0 个 Doc
db.table1.aggregate([
{$project:{
item_id:1,
m_date: 1,
sub_item_id : 1,
ref_id :1
}},
{
$lookup: {
from: 'table2',
localField: 'item_id',
foreignField: 'item_id',
as: 'table2_values'
},
},
{$unwind:'$table2_values'},
{ $group: {
_id: {ref_id: "$table2_values.ref_id", m_date: "$m_date"
,sub_item_id:'$sub_item_id' },
}},
{$project:{_id:0,m_date:'$_id.m_date',ref_id:'$_id.ref_id'
,sub_item_id:'$_id.sub_item_id',item_id:1}},
{
$match: {"table2_values.item_id": "1.1"}
}
])
请帮助我获得上述预期结果
您可以尝试使用 mongodb 3.6
进行以下聚合db.table1.aggregate([
{ "$match": { "item_id": "1.1" }},
{ "$lookup": {
"from": "table2",
"let": { "item_id": "$item_id", "m_date": "$m_date" },
"pipeline": [
{ "$match": {
"$expr": { "$eq": ["$$item_id", "$item_id" ] },
"$expr": { "$eq": ["$$m_date", "$m_date"] }
}}
],
"as": "table2_values"
}},
{ "$addFields": { "ref_id": { "$arrayElemAt": ["$table2_values.ref_id", 0] }}},
{ "$project": { "_id": 0, "item_id": 1, "ref_id": 1 }}
])