Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to do a $lookup aggregation between two databases in Mongodb?

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?

like image 352
Alexander Suraphel Avatar asked Aug 30 '16 08:08

Alexander Suraphel


People also ask

What is $lookup in MongoDB?

$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.

Which aggregation method is preferred for use by MongoDB?

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.

How do I merge two MongoDB aggregate collections?

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.

Does MongoDB have aggregation?

Starting in MongoDB 4.2, you can update documents with an aggregation pipeline if you use the stages shown in Updates with Aggregation Pipeline.


3 Answers

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

like image 99
Clement Amarnath Avatar answered Sep 24 '22 20:09

Clement Amarnath


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

like image 40
Gnopor Avatar answered Sep 21 '22 20:09

Gnopor


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]
            }
        }
    }
])
like image 29
loonis Avatar answered Sep 25 '22 20:09

loonis