Here is a workaround for those who don't use Atlas Data Lake.
Let's assume we have collection1
in db1
and collection2
in db2
.
From db1
, first merge collection2
db.getSiblingDB("db2").collection2.aggregate([
{
$match: { "key1": "optional some condition to limit the number of results" }
},
{
$project: { k2: "$optional projection to limit object attributes" }
},
{
$merge: { into: { db: "db1", coll: "tmpCollection2" } }
}
])
Then use is to lookup with collection1
db.collection1.aggregate([
{
$lookup: {
from: "tmpCollection2",
localField: "localField",
foreignField: "k2",
as: "tmpCollection2_docs"
}
},
{
//Simulate the inner join if needed
$match: {
"tmpCollection2_docs": {
$ne: []
}
}
},
{
// Transform the array if needed
$addFields: {
"tmpCollection2_docs": {
$arrayElemAt: ["$tmpCollection2_docs", 0]
}
}
}
])