mongodb加入多个集合 [英] mongodb join multiple collections

查看:119
本文介绍了mongodb加入多个集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用mongochef在MongoDB中加入" 3个集合.集合是订单",员工"和城市".我尝试使用临时集合,但是它无效. 现在,我在第一个"join"中使用var = a.

I'd like to "join" 3 Collections in MongoDB by using mongochef. The collections are "Order", "Employee" and "City". I tried to use temp collections, but it is not effective. Now I am using var = a for the first "join".

如果我想显示"a",则仅显示20个结果. 您有想法或其他解决方案吗?

If I 'd like to show "a", there are displayed 20 results only. Do you have an idea or another solution?

        var a = db.Order.aggregate([
    { 


      $lookup: 
      {
        from: "City",
        localField: "City Key",
        foreignField: "City Key",
        as: "lsg"
      }
    },
    {
        $unwind: "$lsg"
    },
    {
        $project: 
        {
            "_id":1,
            "Salesperson Key":1,
            "City": "$lsg.City"
        }
    }

    ])

    a;

var b = db.Employee.aggregate([
{ 


  $lookup: 
  {
    from: "a",
    localField: "Employee Key",
    foreignField: "Salesperson Key",
    as: "lsg2"
  }
},
{
    $unwind: "$lsg2"
},
{
    $project: 
    {
        "_id":1,
        "Employee":1
    }
}

])

预先感谢您的答复.

推荐答案

您可以放置​​多个$ lookup阶段,因此可以使用这样的查询(无法对其进行测试,但应该可以使用) 但是您应该避免多次连接,请记住,MongoDB 不是关系数据库...

you can put multiple $lookup stages, so you could use a query like this (could't test it but should work) But you should avoid multiple joins, keep in mind that MongoDB is not a relational database...

db.Order.aggregate([
   {
      $lookup:{
         from:"City",
         localField:"City Key",
         foreignField:"City Key",
         as:"lsg"
      }
   },
   {
      $unwind:"$lsg"
   },
   {
      $lookup:{
         from:"Employee",
         localField:"Salesperson Key",
         foreignField:"Employee Key",
         as:"lsg2"
      }
   },
   {
      $unwind:"$lsg2"
   },
   {
      $project:{
         "_id":1,
         "Employee":1,
         "Salesperson Key":1,
         "City":"$lsg.City"
      }
   }
]);

这篇关于mongodb加入多个集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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