mongodb多表查询(附带pymongo实例)
mongodb有$lookup可以做多表查询
举个例子
数据如下
db.orders.insert([
{ "_id" : , "item" : "almonds", "price" : , "quantity" : },
{ "_id" : , "item" : "pecans", "price" : , "quantity" : },
{ "_id" : }
])
db.inventory.insert([
{ "_id" : , "sku" : "almonds", description: "product 1", "instock" : },
{ "_id" : , "sku" : "bread", description: "product 2", "instock" : },
{ "_id" : , "sku" : "cashews", description: "product 3", "instock" : },
{ "_id" : , "sku" : "pecans", description: "product 4", "instock" : },
{ "_id" : , "sku": null, description: "Incomplete" },
{ "_id" : }
])
聚合操作如下
db.orders.aggregate([
{
$lookup:
{
from: "inventory",
localField: "item",
foreignField: "sku",
as: "inventory_docs"
}
},
])
上面的代码意思是,从order表中取item字段作为inventory表中sku的查询条件,然后把数据保存到inventory_docs字段,
等价于mysql中的
SELECT *, inventory_docs
FROM orders
WHERE inventory_docs IN (SELECT *
FROM inventory
WHERE sku= orders.item);
下面用pymongo实现
from pymongo import MongoClient def test():
client = MongoClient()
db = client[db_name]
db['orders'].aggregate([{'$lookup':{'from': "inventory", "localField": "item", "foreignField": "sku", "as": "inventory_docs"}}])
这样就实现了上述的效果了
上述操作返回以下结果
{
"_id" : ,
"item" : "almonds",
"price" : ,
"quantity" : ,
"inventory_docs" : [
{ "_id" : , "sku" : "almonds", "description" : "product 1", "instock" : }
]
}
{
"_id" : ,
"item" : "pecans",
"price" : ,
"quantity" : ,
"inventory_docs" : [
{ "_id" : , "sku" : "pecans", "description" : "product 4", "instock" : }
]
}
{
"_id" : ,
"inventory_docs" : [
{ "_id" : , "sku" : null, "description" : "Incomplete" },
{ "_id" : }
]
}
看起来蛮方便的,但是其实很麻烦,如果我们只想显示inventory表中的某些字段,这样不符合我们的要求
这时候就需要用到$project和$arrayElemAt
如果我们只想显示inventory中的instock字段的话应该这样做
db.orders.aggregate([
{
$lookup:
{
from: "inventory",
localField: "item",
foreignField: "sku",
as: "inventory_docs"
}
},
{
$project:
{
'instock': {'$arrayElemAt':['$inventory_docs.instock', ]}
},
},
])
返回的结果是
{'_id': 1.0, 'instock': 120.0}
{'_id': 2.0, 'instock': 70.0}
{'_id': 3.0}
之前orders的数据好像没了,不用着急,在project里面添加想要返回的数据项即可
如我想要返回orders的price字段,在$project中添加{'price':1}即可,其他同
lookup更多详细用法
project可以指定返回的内容
$project:
{
'sku':, 'item':,
}
指定返回sku和item
也可以重命名
$project:
{
'test': '$sku',
'item': ,
}
将sku字段重命名为test返回
https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/index.html
mongodb多表查询(附带pymongo实例)的更多相关文章
- mongodb 多表查询
今天有一个业务涉及到mongodb的多表查询,大体记录下语句结构 db.table_a.aggregate([ {$lookup:{from:"table_b",localFiel ...
- SAP内表查询速度优化实例-OPEN SQL
一.FOR ALL ENTRIES IN 案例 今天碰到工单报工统计分析表查询速度特别慢 经查看源代码: SELECT afpo~dwerk afko~aufnr afpo~matnr AS plnb ...
- MongoDB联表查询
表A: id name --------------------------- 1 Tom 2 Roger 3 Mars 4 Brent 表B: id result ----------------- ...
- NodeJs操作MongoDB之多表查询($lookup)与常见问题
NodeJs操作MongoDB之多表查询($lookup)与常见问题 一,方法介绍 aggregate()方法来对数据进行聚合操作.aggregate()方法的语法如下 1 aggregate(ope ...
- Yii框架 多表查询实例
Yii框架多表查询实例:总共分为两个步骤(以下的代码我全部都写在model中):1.先在主表model中声明关联表中所需要查询的字段. public $surveyls_description; // ...
- [重要] Django 多条件多表查询实例问题
当时想做一个多条件查询,但是对于要查询的信息,是分布在不同的表里,这就涉及到了多表查询问题. DjangoBook里提到了一些查询的方式,但是不够全面,就去百度搜了下. 当去网上百度搜多表查询,或多条 ...
- Django 多条件多表查询实例问题
当时想做一个多条件查询,但是对于要查询的信息,是分布在不同的表里,这就涉及到了多表查询问题. DjangoBook里提到了一些查询的方式,但是不够全面,就去百度搜了下. 当去网上百度搜多表查询,或多条 ...
- PHP.36-TP框架商城应用实例-后台12-商品管理-主分类添加、修改、搜索(连表查询)
需求:一个商品必须有一个主分类,一个主分类可以有多个商品 [一对多] 修改表p39_goods,增加外键约束,增加索引 主分类添加[控制器->页面] 1.在控制器GoodsController. ...
- 010.简单查询、分组统计查询、多表连接查询(sql实例)
-------------------------------------day3------------ --添加多行数据:------INSERT [INTO] 表名 [(列的列表)] --SEL ...
随机推荐
- [PAT] 1146 Topological Order(25 分)
This is a problem given in the Graduate Entrance Exam in 2018: Which of the following is NOT a topol ...
- Django Ajax学习一
1. 简单的加法 <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...
- jdk1.6错误:no such provider: BC jdk1.6支持SSL问题
程序调用https请求,由于jdk1.6只支持1024的DH,需要调整 1.在$JAVA_HOME/jre/lib/ext 下添加加密组件包 bcprov-jdk15on-1.52.jar和bcpro ...
- virtualbox测试k8s要注意的情况
想在virtualBox上测试k8s,遇到两个情况要注意.. 第一是flannel和dashborad起不起来,master都无法正常..这时可以想办法把Iptables,selinux,firewa ...
- Pycharm5注册方式 @LYRE}}(T1[DD[@81IZDU$A
0x1 ,安装 0x2 , 调整时间到2038年. 0x3 ,申请30天试用 0x4, 退出pycharm 0x5, 时间调整回来. 注册方法2: 在 注册时选择 License server ...
- http.pieplining
默认情况下http协议中每个传输层连接只能承载一个http请求和响应,然后结束. HTTP是一个简单的协议.客户进程建立一条同服务器进程的 T C P连接,然后发出请求并读取服务器进程的响应.服务器进 ...
- 训练continue
11.16 树状数组 http://codeforces.com/contest/1070/problem/C digit sum+% dp http://codeforces.com/contest ...
- bytes2HexString
public static String bytes2HexString(byte[] b) { String r = ""; for (int i = 0; i < b.l ...
- Struts2中的设计模式
http://blog.csdn.net/significantfrank/article/details/7712053 1. Command Pattern 基本定义: 把Command(Requ ...
- 关于lower_bound的优先级重载
今天才知道$lower\_bound$最后有一个优先级参数…… 首先$lower\_bound$中的优先级和序列优先级必须相同才有效 $lower\_bound$中优先级默认的是小于号,也就是说仅当序 ...