mysql - 使用 $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"
}
}
] )
解决方案
推荐阅读
- opencv - 重建立体图像
- marklogic - MarkLogic 森林 无效的跨设备链接
- python - 如何创建一个简单的 GUI 来获取 2 个 Excel 文件并将它们上传到我的脚本中
- html - 为什么我的电子邮件模板中的图像没有正确对齐?
- python - 使用 Python 中的 PID 检查进程的状态
- angular - 这种轮询可以接受吗(来自 Laravel 的 Angular 轮询数据)?
- python-3.x - 在电子邮件验证码中打印“无”
- python-3.x - 除了子句不执行python
- mongodb - 统计并列出 MongoDB 中某个字段的前 20 个重复次数最多的值
- dictionary - 为什么带有条件的映射总是返回值