本地网站做淘宝客,制作app步骤,房地产首页设计,公司网络营销推广软件MongoDB跨表跨库查询 1.数据准备#xff1a;2.跨集合查询3.跨库查询应该怎么做#xff1f; 讲一个简单的例子#xff0c;python连接mongodb做跨表跨库查询的正确姿势
1.数据准备#xff1a;
use order_db;
db.createCollection(orders);
db.orders.insertMan… MongoDB跨表跨库查询 1.数据准备2.跨集合查询3.跨库查询应该怎么做 讲一个简单的例子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: johnexample.com,address: 123 Main St},{_id: 102,name: Jane Smith,email: janeexample.com,address: 456 Oak Ave},{_id: 103,name: Bob Johnson,email: bobexample.com,address: 789 Pine Blvd}
]);2.跨集合查询
from pymongo import MongoClient## Joint Table Quety
# 连接到 MongoDB 数据库
client MongoClient(mongodb://admin:adminlocalhost: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,样例数据保持不变
from pymongo import MongoClient## Cross Database Query# 连接到 MongoDB 数据库
client_db1 MongoClient(mongodb://admin:adminlocalhost:27017/)
client_db2 MongoClient(mongodb://admin:adminlocalhost:27017/)# 选择数据库和集合
customer_db client_db1.customer_db
order_db client_db2.order_dbcustomers_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_idmatching_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)
结果符合预期