在Mongodb中的两个数据库之间是否可以进行$ lookup聚合? [英] Is it possible to do a $lookup aggregation between two databases in Mongodb?

查看:347
本文介绍了在Mongodb中的两个数据库之间是否可以进行$ lookup聚合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试执行以下操作:

I'm trying to do something like this:

use user; 

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

userorganization在两个不同的数据库中.

user and organization are in two different databases.

如果不可能的话,还有哪些替代方案?

推荐答案

是否可以在两个数据库之间进行$ lookup聚合 Mongodb?

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

不可能在两个不同的数据库中使用查找进行查询. mongodb中的 $ lookup 支持对未分片集合执行左外部联接在同一个数据库中.

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>
     }
}

我们可以使用getSibling("dbname")从一个数据库中查询另一个数据库

We can use getSibling("dbname") to query another db from one db

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

参考- MongoDB跨数据库查询

这篇关于在Mongodb中的两个数据库之间是否可以进行$ lookup聚合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆