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

查看:49
本文介绍了是否可以在 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天全站免登陆