32

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?

Alexander Suraphel
  • 10,103
  • 10
  • 55
  • 90

3 Answers3

20

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

Community
  • 1
  • 1
Clement Amarnath
  • 5,301
  • 1
  • 21
  • 34
9

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

Steven
  • 1,996
  • 3
  • 22
  • 33
Gnopor
  • 587
  • 9
  • 16
5

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]
            }
        }
    }
])
loonis
  • 1,317
  • 16
  • 19