Is it possible to aggregate $ lookup between two databases in Mongodb?

I am trying to do something like this:

use user; 

db.user.aggregate([
    {
      $lookup:
        {
          from: "organization.organization",
          localField: "organizationId",
          foreignField: "uuid",
          as: "user_org"
        }
   }
])

userand organizationare in two different databases.

If this is not possible, what are the alternatives?

+4
source share
1 answer

Is it possible to aggregate $ lookup between two databases in MongoDB?

It is not possible to request the use of search in two different db. $ lookup in mongodb support Performs a left outer join in an insecure 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 request another db from one db

db.getSiblingDB('test').foo.find()

- - MongoDB

+4

Source: https://habr.com/ru/post/1652966/


All Articles