python mongodb lookup关联查询

$lookup 是 Mongodb 3.2版本 新增的聚合框架中的一种查询方式; 主要用来实现多表链接查询; 相当关系型数据库中多表链接查询。

  1. 主要功能 是将每个输入待处理的文档,经过$lookup 阶段的处理,输出的新文档中会包含一个新生成的数组列(户名可根据需要命名新key的名字 )。数组列存放的数据 是 来自 被Join 集合的适配文档,如果没有,集合为空(即 为[ ])
字段 描述
from 同一个database中用于连接查询的collection的名称
localField 源集合中的match值,如果输入的集合中,某文档没有 localField这个Key(Field),在处理的过程中,会默认为此文档含有 localField:null的键值对。
foreignField 存在于from那个集合的外键
as 别名
let 定义参数,在pipeline中使用
pipeline 对join进来的数据做筛选
  1. 例子、插入orders数据
python 复制代码
import pymongo
import random
import string
from pymongo.collation import Collation
random.seed(10)
letters = string.ascii_lowercase
upper = string.ascii_uppercase


class MongoDBServer():
    def __init__(self,database,collation) -> None:
        self.client = pymongo.MongoClient('mongodb://ellis:ellischen@192.168.214.133:32000/')
        self.database = self.client[database]
        
        self.colleceion = self.database[collation]
        
    def insert_many(self,documents):
        self.colleceion.insert_many(documents)


server = MongoDBServer('lookup','orders')

server.insert_many([
   { "_id" : 1, "item" : "almonds", "price" : 12, "quantity" : 2 },
   { "_id" : 2, "item" : "pecans", "price" : 20, "quantity" : 1 },
   { "_id" : 3  }
] )

插入inventory数据

python 复制代码
import pymongo
import random
import string
from pymongo.collation import Collation
random.seed(10)
letters = string.ascii_lowercase
upper = string.ascii_uppercase


class MongoDBServer():
    def __init__(self,database,collation) -> None:
        self.client = pymongo.MongoClient('mongodb://ellis:ellischen@192.168.214.133:32000/')
        self.database = self.client[database]
        
        self.colleceion = self.database[collation]
        
    def insert_many(self,documents):
        self.colleceion.insert_many(documents)
        
    def lookup(self,destination,localField,foreignField,as_field):
        return self.colleceion.aggregate([{"$lookup":{"from":destination,"localField":localField,"foreignField":foreignField,"as":as_field}}])


server = MongoDBServer('lookup','inventory')

server.insert_many([
   { "_id" : 1, "sku" : "almonds", "description": "product 1", "instock" : 120 },
   { "_id" : 2, "sku" : "bread", "description": "product 2", "instock" : 80 },
   { "_id" : 3, "sku" : "cashews", "description": "product 3", "instock" : 60 },
   { "_id" : 4, "sku" : "pecans", "description": "product 4", "instock" : 70 },
   { "_id" : 5, "sku": None, "description": "Incomplete" },
   { "_id" : 6 }
] )

inventory 和orders通过item以及sku两个字段关联

  1. 使用lookup查询
python 复制代码
import pymongo
import random
import string
from pymongo.collation import Collation
random.seed(10)
letters = string.ascii_lowercase
upper = string.ascii_uppercase


class MongoDBServer():
    def __init__(self,database,collation) -> None:
        self.client = pymongo.MongoClient('mongodb://ellis:ellischen@192.168.214.133:32000/')
        self.database = self.client[database]
        
        self.colleceion = self.database[collation]
        
    def insert_many(self,documents):
        self.colleceion.insert_many(documents)
        
    def lookup(self,destination,localField,foreignField,as_field):
        return self.colleceion.aggregate([{"$lookup":{"from":destination,"localField":localField,"foreignField":foreignField,"as":as_field}}])


server = MongoDBServer('lookup','orders')

for item in server.lookup("inventory","item","sku","inventory_docs"):
    print(item)
  1. lookup 与array类型一起使用
python 复制代码
import pymongo
import random
import string
from pymongo.collation import Collation

random.seed(10)
letters = string.ascii_lowercase
upper = string.ascii_uppercase


class MongoDBServer():
    def __init__(self,database,collation) -> None:
        self.client = pymongo.MongoClient('mongodb://ellis:ellischen@192.168.214.133:32000/')
        self.database = self.client[database]
        
        self.colleceion = self.database[collation]
        
    def insert_many(self,documents):
        self.colleceion.insert_many(documents)
        
    def lookup(self,destination,localField,foreignField,as_field):
        return self.colleceion.aggregate([{"$lookup":{"from":destination,"localField":localField,"foreignField":foreignField,"as":as_field}}])


server = MongoDBServer('lookup','classes')

server.insert_many([
   { "_id": 1, "title": "Reading is ...", "enrollmentlist": [ "giraffe2", "pandabear", "artie" ], "days": ["M", "W", "F"] },
   { "_id": 2, "title": "But Writing ...", "enrollmentlist": [ "giraffe1", "artie" ], "days": ["T", "F"] }
] )


server = MongoDBServer('lookup','members')

server.insert_many([
   { "_id": 1, "name": "artie",  "status": "A" },
   { "_id": 2, "name": "giraffe", "status": "D" },
   { "_id": 3, "name": "giraffe1",  "status": "A" },
   { "_id": 4, "name": "panda", "status": "A" },
   { "_id": 5, "name": "pandabear",  "status": "A" },
   { "_id": 6, "name": "giraffe2",  "status": "D" }
]  )
for item in server.lookup('members','enrollmentlist','name',"haha"):
    print(item)
  1. let以及pipeline的使用
python 复制代码
import pymongo
import random
import string
from pymongo.collation import Collation

random.seed(10)
letters = string.ascii_lowercase
upper = string.ascii_uppercase


class MongoDBServer():
    def __init__(self,database,collation) -> None:
        self.client = pymongo.MongoClient('mongodb://ellis:ellischen@192.168.214.133:32000/')
        self.database = self.client[database]
        
        self.colleceion = self.database[collation]
        
    def insert_many(self,documents):
        self.colleceion.insert_many(documents)
        
    def lookup(self,destination,localField,foreignField,as_field):
        return self.colleceion.aggregate([{"$lookup":{"from":destination,"localField":localField,"foreignField":foreignField,"as":as_field}}])


server = MongoDBServer('lookup','orders')


for item in server.colleceion.aggregate([
    {
        "$lookup":{
            "from": "inventory",
            "localField": "item",
            "foreignField": "sku",
            "let": {
                "instock": 100
            },
            "pipeline": [
                { "$match":
                    { "$expr":
                        { "$gt": [ "$instock", "$$instock"] }
                    }
                }
            ],
            "as": "haha"
        }
    },
    # {"$project":{"item":1,"price":1,"number":{"$size":"$haha"}}},
    # {"$match":{"number":{"$gte":1}}}
    {
        "$match":{"$expr":{"$gte":[{"$size":"$haha"}, 1]}}
    }
    
    
]):
    print(item)

https://www.mongodb.com/docs/manual/reference/operator/aggregation/lookup/

相关推荐
马克Markorg3 小时前
常见的向量数据库和具有向量数据库能力的数据库
数据库
冷雨夜中漫步3 小时前
Python快速入门(6)——for/if/while语句
开发语言·经验分享·笔记·python
郝学胜-神的一滴3 小时前
深入解析Python字典的继承关系:从abc模块看设计之美
网络·数据结构·python·程序人生
百锦再3 小时前
Reactive编程入门:Project Reactor 深度指南
前端·javascript·python·react.js·django·前端框架·reactjs
喵手5 小时前
Python爬虫实战:旅游数据采集实战 - 携程&去哪儿酒店机票价格监控完整方案(附CSV导出 + SQLite持久化存储)!
爬虫·python·爬虫实战·零基础python爬虫教学·采集结果csv导出·旅游数据采集·携程/去哪儿酒店机票价格监控
Coder_Boy_5 小时前
技术让开发更轻松的底层矛盾
java·大数据·数据库·人工智能·深度学习
2501_944934735 小时前
高职大数据技术专业,CDA和Python认证优先考哪个?
大数据·开发语言·python
helloworldandy5 小时前
使用Pandas进行数据分析:从数据清洗到可视化
jvm·数据库·python
肖永威7 小时前
macOS环境安装/卸载python实践笔记
笔记·python·macos
TechWJ7 小时前
PyPTO编程范式深度解读:让NPU开发像写Python一样简单
开发语言·python·cann·pypto