首页 > 解决方案 > 使用 $Lookup 将 MYSQL 查询转换为 mongodb

问题描述

我是 Mongodb 的新手,我现在必须将我的 SQL 查询移动到 mongodb。我阅读了该文档并获得了有关编写 mongodb 查询的足够知识,但是来自 MYSQL 需要时间来获得有关构建 Mongo 查询的专业知识。

我现在正在尝试使用 $Lookup 将此查询转换为 Mongo

select * from orig_tbl emas
inner join  cust-tbl_after_cleanup tmclient
on emas.state = tmclient.province
and emas.cons_city = tmclient.city
where 
emas.state = 'VT' and
emas.can_match_address like concat('%',tmclient.street,'%')
and emas.can_cmp_name like concat('%',tmclient.name,'%')
and emas.can_match_address like concat('%',tmclient.house_num,'%')
and emas.can_match_address like concat('%',tmclient.postal_code,'%')
and emas.cons_city like concat('%',tmclient.city,'%')
and emas.State like concat('%',tmclient.province,'%')
group by tmclient.id,tmclient.name

如您所见,我正在对多个条件进行内部连接,然后对 cloumns 的数量进行部分字符串匹配。从文档中我知道了如何进行多条件连接,但不确定如何进行部分字符串匹配

到目前为止的代码:

aggregate(

    [
    { 
         '$match' : { 'name' : 'Walmart' } 
    },
    { 
        '$lookup': 
            {
  'from': 'entity_map_all_states',
  'let': { 'order_city': "$city", 'order_qty': "$province" },
           'pipeline': [
              { '$match':
                 { '$expr':
                    { '$and':
                       [
                         { '$eq': [ "$cons_city",  "$$order_city" ] },
                         { '$eq': [ "$State", "$$order_qty" ] }
                       ]
                    }
                 }
              }
           ],
           'as': "stockdata"
         }
    }
    ] )

标签: mysqlmongodbmongodb-queryaggregation-framework

解决方案


推荐阅读