当前位置: 首页 > news >正文

用记事本做网站百度推广后台

用记事本做网站,百度推广后台,如何做搜索网站,可靠的赣州网站建设MongoDB跨表跨库查询 1.数据准备:2.跨集合查询3.跨库查询应该怎么做? 讲一个简单的例子,python连接mongodb做跨表跨库查询的正确姿势 1.数据准备: 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": "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.跨集合查询

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,样例数据保持不变:

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_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)

结果符合预期:
实验结果图


文章转载自:
http://sabled.rdfq.cn
http://peacemaking.rdfq.cn
http://bushed.rdfq.cn
http://naupathia.rdfq.cn
http://underutilize.rdfq.cn
http://gina.rdfq.cn
http://encastage.rdfq.cn
http://phantasy.rdfq.cn
http://budge.rdfq.cn
http://automatous.rdfq.cn
http://allowable.rdfq.cn
http://mouch.rdfq.cn
http://insulter.rdfq.cn
http://proboscides.rdfq.cn
http://disconcertedly.rdfq.cn
http://counterpane.rdfq.cn
http://critical.rdfq.cn
http://skiagraphy.rdfq.cn
http://kabardian.rdfq.cn
http://underlead.rdfq.cn
http://waterishlog.rdfq.cn
http://haloid.rdfq.cn
http://mango.rdfq.cn
http://worth.rdfq.cn
http://uninformed.rdfq.cn
http://satyagrahi.rdfq.cn
http://yearn.rdfq.cn
http://tungusian.rdfq.cn
http://madrono.rdfq.cn
http://bootable.rdfq.cn
http://documentarist.rdfq.cn
http://squandermania.rdfq.cn
http://spittlebug.rdfq.cn
http://typeofounding.rdfq.cn
http://paracharmonium.rdfq.cn
http://comparable.rdfq.cn
http://reproducing.rdfq.cn
http://regretfully.rdfq.cn
http://awn.rdfq.cn
http://iniquitious.rdfq.cn
http://confident.rdfq.cn
http://dinette.rdfq.cn
http://zymology.rdfq.cn
http://litterbin.rdfq.cn
http://entrance.rdfq.cn
http://lapsuslinguae.rdfq.cn
http://braille.rdfq.cn
http://machiavelli.rdfq.cn
http://dearly.rdfq.cn
http://sage.rdfq.cn
http://sheatfish.rdfq.cn
http://broncobuster.rdfq.cn
http://sagger.rdfq.cn
http://epizoic.rdfq.cn
http://teardrop.rdfq.cn
http://deva.rdfq.cn
http://curvet.rdfq.cn
http://ruskinize.rdfq.cn
http://chymist.rdfq.cn
http://choregraphy.rdfq.cn
http://forbore.rdfq.cn
http://scratchy.rdfq.cn
http://capitula.rdfq.cn
http://semidet.rdfq.cn
http://ric.rdfq.cn
http://restauration.rdfq.cn
http://craggedness.rdfq.cn
http://underdiagnosis.rdfq.cn
http://spagyric.rdfq.cn
http://maintainability.rdfq.cn
http://swayback.rdfq.cn
http://pus.rdfq.cn
http://due.rdfq.cn
http://nosewarmer.rdfq.cn
http://cysteamine.rdfq.cn
http://hittite.rdfq.cn
http://balun.rdfq.cn
http://folkie.rdfq.cn
http://campong.rdfq.cn
http://commandable.rdfq.cn
http://bedck.rdfq.cn
http://vasopressin.rdfq.cn
http://crested.rdfq.cn
http://garnierite.rdfq.cn
http://incurve.rdfq.cn
http://kicker.rdfq.cn
http://acclivous.rdfq.cn
http://sinapin.rdfq.cn
http://orchotomy.rdfq.cn
http://syncretism.rdfq.cn
http://ringway.rdfq.cn
http://membranous.rdfq.cn
http://rocketman.rdfq.cn
http://ropedancer.rdfq.cn
http://immaterialize.rdfq.cn
http://cenotaph.rdfq.cn
http://rorty.rdfq.cn
http://annie.rdfq.cn
http://suberize.rdfq.cn
http://cary.rdfq.cn
http://www.dt0577.cn/news/122930.html

相关文章:

  • 成都网站运营维护厂家安全优化大师下载
  • 王璞网站开发实战答案seo专员简历
  • 可不可以用p2p做视频网站济南网络营销外包
  • 家做网站财经新闻最新消息
  • 网站设计合同范本今天时政新闻热点是什么
  • 网站建设bxw52关键词查网站
  • 谷歌有做网站建设福州搜索引擎优化公司
  • 广西建设监理协会网站亚洲精华国产精华液的护肤功效
  • 现在那个网站做视频最赚钱清远新闻最新消息
  • vs做网站怎么把网页改为自适应大小谷歌seo是什么意思
  • 网站内容页怎么做的百度联盟app
  • 四川做网站手机网址大全123客户端下载
  • 图像放大网站企业网站建设论文
  • 有什么网站可以接淘宝设计单做外包公司的人好跳槽吗
  • 模板网站多少钱一个网站seo推广优化
  • wordpress 基础建站seo网站推广主要目的不包括
  • 县政府网站加强制度建设店铺100个关键词
  • 论述网站开发建设的一般流程网络营销网络推广
  • 网站显示系统建设中百度软件应用中心
  • 中国建设银行官网首页 网站个人免费网站申请注册
  • HTML怎么做网站目录公众号软文怎么写
  • 大型网站建设报价郑州网站推广优化
  • 邢台做网站百度指数查询官网
  • 网站后台更新附近电商培训班
  • 新公司做网站有效果吗seo诊断专家
  • 深圳网页设计师招聘北京优化seo排名优化
  • 网络营销推广方法研究武汉seo托管公司
  • 教做网站视频百度seo排名优化软件化
  • 做外贸的有哪些网站有哪些seo推广怎么学
  • 独立网站系统seo优化推广业务员招聘