sql to mongoDB mapping chart 一些比较简单的官方文档例子,已经有的就不再写例子
https://docs.mongodb.com/manual/reference/sql-comparison/
本教程主要是讲aggregate的一些函数实现左联结的用法,用到的函数有
$match,$unwind,$project,$let,$cond,$redact等
准备测试数据
Oracle数据库 初始数据,address/custInfo表:
custInfo
| _id | nameame | age | gender |
|---|---|---|---|
| 1001 | sum | 20 | F |
| 1002 | stone | 21 | M |
| 1003 | kral | 22 | M |
| 1004 | git | 22 | M |
address
| _id | status | custId | region | homeAdd |
|---|---|---|---|---|
| 1 | A | 1001 | GuangZhou | 天河北路 |
| 2 | A | 1001 | ShangHai | 天河南路 |
| 3 | A | 1002 | BeiJing | 天安门 |
| 4 | B | 1002 | 沙河路17号 | |
| 5 | B | 1004 | GuangZhou | 沙河路18号 |
Mongo数据库 表结构 (假设数据和oracle数据库的数据一样)
custInfo表
{
_id:"1001",
name:"sum",
age:20,
gender:"F",
address:[
{
status:"A",
region:"GuangZhou",
homeAdd:"天河北路"
},
{
status:"A",
region:"ShangHai",
homeAdd:"天河南路"
}
]
}
练习一:左联结
sql
select cu._id as custId,cu.name,ad.region,ad.homeAdd,ad.status
from custInfo cu
left join address ad on cu._id=ad.custId;
mongoQuery
db.custInfo.aggregate([
{$unwind:{
path:"$address",
preserveNullAndEmptyArrays:true}
},
{$project:{
_id:1,
name:1,
region:"$address.region",
homeAdd:"$address.homeAdd",
status:"$address.status"
}
}
]);
查询结果
| custId | name | region | homeAdd | status |
|---|---|---|---|---|
| 1001 | sum | GuangZhou | 天河北路 | A |
| 1001 | sum | ShangHai | 天河南路 | A |
| 1002 | stone | BeiJing | 天安门 | A |
| 1002 | stone | 沙河路17号 | B | |
| 1003 | kral | |||
| 1004 | git | GuangZhou | 沙河路17号 | B |
练习二:左联结+where条件
sql
select cu._id as custId,cu.name,ad.region,ad.homeAdd,ad.status from custInfo cu
left join address ad on cu._id=ad.custId
where ad.status='A';
mongoQuery
db.custInfo.aggregate([
{$unwind:{
path:"$address",
preserveNullAndEmptyArrays:true}
},
{$match:{"address.status":"A"}},
{$project:{
_id:1,
name:1,
region:"$address.region",
homeAdd:"$address.homeAdd",
status:"$address.status"
}
}
]);
查询结果
| custId | name | region | homeAdd | status |
|---|---|---|---|---|
| 1001 | sum | GuangZhou | 天河北路 | A |
| 1001 | sum | ShangHai | 天河南路 | A |
| 1002 | stone | BeiJing | 天安门 | A |
练习三:左联结+联结条件
sql
select cu._id as custId,cu.name,ad.region,ad.homeAdd,ad.status
from custInfo cu
left join address ad on cu._id=ad.custId and ad.status='A';
mongoQuery
db.custInfo.aggregate([
{$unwind:{
path:"$address",
preserveNullAndEmptyArrays:true}
},
{$project:{
_id:1,
name:1,
items:"$address",
acStatus:{
$let:{
vars:{},
in:{
$cond:{
if:{$eq:["$address.status","A"]},
then:"$address.status",
else:"delete"
}
}
}
}
}
},
{$group:{
_id:{
_id:"$_id",
name:"$name",
gender:"$gender",
},
itemsSold:{
$push:{
status:"$items.status",
region:"$items.region",
homeAdd:"$items.homeAdd",
deleteflag:"$acStatus"
}
}
}
},
{
$redact: {
$cond: {
if: {$eq:["$deleteflag","delete"]
},
then: "$$PRUNE",
else: "$$DESCEND"
}
}
},
{$unwind:{
path:"$itemsSold",
preserveNullAndEmptyArrays:true}
},
{$project:{
_id:0,
custId:"$_id._id",
name:"$_id.name",
region:"$itemsSold.region",
homeAdd:"$itemsSold.homeAdd",
status:"$itemsSold.status"
}
},
{$sort:{"custId":1}}
]);
查询结果
| custId | name | region | homeAdd | status |
|---|---|---|---|---|
| 1001 | sum | GuangZhou | 天河北路 | A |
| 1001 | sum | ShangHai | 天河南路 | A |
| 1002 | stone | BeiJing | 天安门 | A |
| 1003 | karl | |||
| 1004 | git |
在这里说明一下,mongodb 不支持左联结,所以的使用其他思路去解决这个问题。简单总结一下就是通过$redact函数的$PRUNE清除数据的功能,这里需要具体讲一下$redact函数的用法,下回分解
created Date 2017年5月29日 19:24:26
这篇教程探讨如何在MongoDB中使用aggregate函数模拟SQL的左联结操作,涉及$match、$unwind、$project等函数的用法,并通过实例展示了不同类型的左联结查询,包括基础左联结、带where条件的左联结以及带有联结条件的左联结。由于MongoDB不直接支持左联结,文章通过$redact函数的$PRUNE特性来实现类似效果。
1636

被折叠的 条评论
为什么被折叠?



