如何在mongoDB中编写联合查询 [英] How to write union queries in mongoDB

查看:604
本文介绍了如何在mongoDB中编写联合查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用与SQL查询类似的2个或更多集合在Mongo DB中编写联合查询?

Is it possible to write union queries in Mongo DB using 2 or more collections similar to SQL queries?

我正在使用spring mongo模板,在我的用例中,我需要根据某些条件从3-4个集合中获取数据.我们可以一次完成此操作吗?

I'm using spring mongo template and in my use case, I need to fetch the data from 3-4 collections based on some conditions. Can we achieve this in a single operation?

例如,我有一个名为"circuitId"的字段,该字段存在于所有4个集合中.而且我需要从该字段与给定值匹配的所有4个集合中获取所有记录.

For example, I have a field named "circuitId" which is present in all 4 collections. And I need to fetch all records from all 4 collections for which that field matches with a given value.

推荐答案

不幸的是,基于文档的MongoDB不像关系数据库引擎中那样支持JOINS/Unions. MongoDB上的关键设计原则之一是防止根据应用程序的数据获取模式使用嵌入式文档进行联接. 话虽如此,如果您确实需要使用这4个集合,则需要管理应用程序端的逻辑,或者您可以按照MongoDB最佳实践重新设计数据库设计.

Unfortunately document based MongoDB doesn't support JOINS/Unions as in Relational DB engines. One of the key design principles on MongoDB is to prevent joins using embedded documents as per your application's data fetch patterns. Having said that, you will need to manage the logic in your application end if you really need to use the 4 collections or you may redesign your DB design as per MongoDB best practices.

有关更多信息: https://docs.mongodb.com/主/核心/数据模型设计/

这篇关于如何在mongoDB中编写联合查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆