22

我正在尝试做这样的事情:

use user; 

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

user并且organization在两个不同的数据库中。

如果这是不可能的,有什么替代方案?

4

2 回答 2

18

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

无法在两个不同的数据库中使用查找进行查询。 mongodb 中的$lookup支持对同一数据库中的非分片集合执行左外连接。

{
   $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")从一个数据库查询另一个数据库

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

参考——MongoDB跨数据库查询

于 2016-08-30T09:05:52.247 回答
5

是的,只需阅读以下 mongodb 文档:

在 Atlas Data Lake 中,$lookup可用于执行来自不同数据库的集合的连接。

https://docs.mongodb.com/datalake/reference/pipeline/lookup-stage

于 2021-02-05T06:35:16.533 回答