【MongoDB】跨库跨表查询(python版)

MongoDB跨表跨库查询

讲一个简单的例子,python连接mongodb做跨表跨库查询的正确姿势

1.数据准备:

复制代码
use order_db;
db.createCollection("orders");
db.orders.insertMany(
    [
  {
    "_id": 1,
    "order_number": "ORD123",
    "product": "Laptop",
    "customer_id": 101
  },
  {
    "_id": 2,
    "order_number": "ORD124",
    "product": "Smartphone",
    "customer_id": 102
  },
  {
    "_id": 3,
    "order_number": "ORD125",
    "product": "Tablet",
    "customer_id": 103
  }
]
    )

use customer_db;
db.createCollection("customers");
db.customers.insertMany(
    [
  {
    "_id": 101,
    "name": "John Doe",
    "email": "john@example.com",
    "address": "123 Main St"
  },
  {
    "_id": 102,
    "name": "Jane Smith",
    "email": "jane@example.com",
    "address": "456 Oak Ave"
  },
  {
    "_id": 103,
    "name": "Bob Johnson",
    "email": "bob@example.com",
    "address": "789 Pine Blvd"
  }
]
    );

2.跨集合查询

python 复制代码
from pymongo import MongoClient

## Joint Table Quety
# 连接到 MongoDB 数据库
client = MongoClient("mongodb://admin:admin@localhost:27017/")

# 选择数据库和集合
db_orders = client.order_db.orders
db_customers = client.customer_db.customers

# 执行跨表查询
pipeline = [
    {
        "$lookup": {
            "from": "customers",
            "localField": "customer_id",
            "foreignField": "_id",
            "as": "customer_info"
        }
    },
    {
        "$unwind": "$customer_info"
    },
    {
        "$project": {
            "_id": 1,
            "order_number": 1,
            "product": 1,
            "customer_info.name": 1,
            "customer_info.email": 1
        }
    }
]

result = list(db_orders.aggregate(pipeline))

# 打印结果
for order in result:
    print(order)

分析:

经过代码测试会发现,pipeline走到lookup结束,customer_info为空,lookup是作用于单个数据库下的不同集合之间的联合查询,但不支持跨库,而网络上充斥着所谓支持跨库查询的案例。。。

因此,将collection放于同一个db下,发现结果符合预期。

3.跨库查询

应该怎么做?

思考:想象我们做的业务,通常都是模块化的,之间都是通过服务/应用层接口调用来实现的,其底层正对应着不同的数据库。比如常见的订单系统和用户系统,因为集中式管理(单个数据库)容易造成性能瓶颈,会按业务进行合理拆分,也更容易复用和拓展。

所以,所谓的跨库查询,实际上就跟业务之间的通信是类似的,这里并不是单库下的主外键查询问题,而是实际场景中多库下多个服务之间的数据互通与一致性查询问题,一般处理手段是将一些联合查询问题放到业务层解决,当然,针对做不同数据库的相同表做同步复制也是可以的,不过显然这与业务拆分的初衷相违背了。

以下是简单的sample,样例数据保持不变:

python 复制代码
from pymongo import MongoClient

## Cross Database Query

# 连接到 MongoDB 数据库
client_db1 = MongoClient("mongodb://admin:admin@localhost:27017/")
client_db2 = MongoClient("mongodb://admin:admin@localhost:27017/")

# 选择数据库和集合
customer_db = client_db1.customer_db
order_db = client_db2.order_db

customers_collection = customer_db.customers
orders_collection = order_db.orders

# 查询 orders 数据
orders_data = list(orders_collection.find())
# 查询 customers 数据
customers_data_dict = {customer["_id"]: customer for customer in customers_collection.find()}
# 手动关联数据
result = []
for order in orders_data:
    customer_id = order.get("customer_id")
    # 在 customers 数据中查找匹配的 customer_id
    matching_customer = customers_data_dict.get(customer_id)
    if matching_customer:
        # 合并数据
        merged_data = {**order, "customer_info": matching_customer}
        result.append(merged_data)

# 打印结果
for item in result:
    print(item)

结果符合预期:

相关推荐
Mr. zhihao26 分钟前
深入解析redis基本数据结构
数据结构·数据库·redis
m0_7488394928 分钟前
利用天正暖通CAD快速掌握风管数量统计的方法
数据库
随身数智备忘录28 分钟前
什么是设备管理体系?设备管理体系包含哪些核心模块?
网络·数据库·人工智能
彦为君33 分钟前
Agent 安全:从权限提示到沙箱隔离
python·ai·ai编程
PILIPALAPENG1 小时前
Python 语法速成指南:前端开发者视角(JS 类比版)
前端·人工智能·python
海市公约1 小时前
MySQL更新语句执行全流程:从Buffer Pool修改到二阶段提交
数据库·mysql·binlog·innodb·undo log·二阶段提交·update执行原理
颂love2 小时前
MySQL的执行流程
android·数据库·mysql
用户8356290780512 小时前
Python 操作 PowerPoint 页眉与页脚指南
后端·python
程序leo源2 小时前
Qt窗口详解
开发语言·数据库·c++·qt·青少年编程·c#
这个DBA有点耶2 小时前
COUNT进阶:超大表的近似计数与HyperLogLog
数据库·sql·程序人生·学习方法·dba·改行学it