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

旧时模样 提交于 2019-11-27 03:18:05

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/39222798/is-it-possible-to-do-a-lookup-aggregation-between-two-databases-in-mongodb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!