我是Mongodb的新手,现在我必须将SQL查询移到mongodb。我阅读了该文档,并获得了有关编写mongodb Queries的足够知识,但是从MYSQL那里获得框架的专业知识将需要一些时间。
我现在尝试使用$ 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"
}
}
] )