I'm trying to do something like this:
use user;
db.user.aggregate([
{
$lookup:
{
from: "organization.organization",
localField: "organizationId",
foreignField: "uuid",
as: "user_org"
}
}
])
user
and organization
are in two different databases.
If this is not possible, what are the alternatives?
$lookup performs an equality match on the localField to the foreignField from the documents of the from collection. If an input document does not contain the localField , the $lookup treats the field as having a value of null for matching purposes.
The pipeline provides efficient data aggregation using native operations within MongoDB, and is the preferred method for data aggregation in MongoDB. The aggregation pipeline can operate on a sharded collection. The aggregation pipeline can use indexes to improve its performance during some of its stages.
For performing MongoDB Join two collections, you must use the $lookup operator. It is defined as a stage that executes a left outer join with another collection and aids in filtering data from joined documents. For example, if a user requires all grades from all students, then the below query can be written: Students.
Starting in MongoDB 4.2, you can update documents with an aggregation pipeline if you use the stages shown in Updates with Aggregation Pipeline.
Is it possible to do a $lookup aggregation between two databases in Mongodb?
It is not possible to query using lookup in two different db's. $lookup in mongodb supports Performs a left outer join to an unsharded collection in the same database.
{ $lookup: { from: <collection to join>, localField: <field from the input documents>, foreignField: <field from the documents of the "from" collection>, as: <output array field> } }
We can use getSibling("dbname")
to query another db from one db
db.getSiblingDB('test').foo.find()
Reference - MongoDB cross database query
Yes just read the following mongodb doc:
In Atlas Data Lake, $lookup
can be used to perform a join of collections from different databases.
https://docs.mongodb.com/datalake/reference/pipeline/lookup-stage
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]
}
}
}
])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With