Nosql多对多

发布于 2024-10-10 18:10:51 字数 32 浏览 1 评论 0原文

在文档数据库设计中处理多对多关系的公认模式是什么?

What is the accepted pattern for handling many-to-many relationships in a document database design?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘香 2024-10-17 18:10:51

您想要如何对多对多进行建模取决于您想要提出什么样的查询、您想要如何更新数据等等...假设我们有与多对多方式的 bar 相关的 foo。

您可以将 foo 建模为

{
   'bars': ['bar1', 'bar2', 'bar3']
}

并将 bar 建模为

{
   'foos': ['foo_x', 'foo_y', 'foo_z']
}

或者您可以将 foo 和 bar 之间的图表或关系建模为单独的文档本身

{
    from: 'foo1',
    to: 'bar1'
}

{
   from: 'foo1',
   to: 'bar2'
}

{  
   from: 'foo2',
   to: 'bar3
}

{
  from 'foo3',
  to: 'bar3'
}

还有很多其他方法。您想要如何做到这一点取决于您想要提出的问题、您想要支持的操作、您想要提高效率以及数据库中可用的索引。

How you want to model the many-to-many will depend on what kind of queries you want to ask, how you want to update the data, etc... Say we have foos related to bars in a many to many fashion.

You could model a foo as

{
   'bars': ['bar1', 'bar2', 'bar3']
}

and model a bar as

{
   'foos': ['foo_x', 'foo_y', 'foo_z']
}

Or you could model the graph or relations between foo and bar as individual documents themselves

{
    from: 'foo1',
    to: 'bar1'
}

{
   from: 'foo1',
   to: 'bar2'
}

{  
   from: 'foo2',
   to: 'bar3
}

{
  from 'foo3',
  to: 'bar3'
}

There are plenty of other ways too. How you want to do it will depend on the questions you want to ask, the operations you want to support, what you want to be efficient, and the indexing available in the database.

蛮可爱 2024-10-17 18:10:51

假设我们讨论的是确实需要关系的情况,而不是仅仅因为 SQL 比复杂对象更好地处理关系而存在的情况,那么设计类似于 SQL 的标准设计 - 两个一对多关系。

主要区别在于您具有多值字段,因此每个文档中都有一个 id 列表,而不是使用第三个文档/表将单个连接记录为一对 id。

如果您遇到该列表太长的情况,您可能会考虑通过搜索索引比关系更好地处理某些内容。

Assuming we are talking about cases where a relationship is really necessary rather than the ones which only exist because SQL handles relationships better than complex objects, the design is similar to the standard one for SQL - two one to many relationships.

The key difference is that you have multivalue fields, so instead of a third document/table recording single connections as a pair of ids, you have a list of ids in each document.

If you run into cases where that list gets too long you are likely looking at something that would be better handled by search indexing than a relationship.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文