小编Ann*_*nne的帖子

如何使用条件加入另外两个集合

select tb1.*,tb3 from tb1,tb2,tb3
 where tb1.id=tb2.profile_id and tb2.field='<text>'
 and tb3.user_id = tb2.id and tb3.status =0
Run Code Online (Sandbox Code Playgroud)

实际上我将sql转换为mongosql,如下所示

mongo 我用过的sql

db.getCollection('tb1').aggregate
([
  { $lookup: 
           { from: 'tb2', 
             localField: 'id', 
             foreignField: 'profile_id', 
             as: 'tb2detail' 
            } 
   },

   { $lookup: 
            { from: 'tb3', 
              localField: 'tb2.id', 
              foreignField: 'user_id', 
              as: 'tb3details' 
            } 
    },

{ $match: 
        { 'status': 
                  { '$ne': 'closed' 
                  }, 
          'tb2.profile_type': 'agent', 
          'tb3.status': 0 
         } 
}

])
Run Code Online (Sandbox Code Playgroud)

但没有达到预期的结果..

任何帮助将不胜感激..

mongodb mongodb-query aggregation-framework

4
推荐指数
1
解决办法
3598
查看次数