在mongodb中编辑子文档N-N关系 [英] editing subdocments N-N relationship in mongodb

查看:120
本文介绍了在mongodb中编辑子文档N-N关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以将文章链接到多个平台的应用程序.

I have an application where an article can be linked to multiple platforms.

文章包含平台列表,平台也包含文章列表.

Article contains a list of platforms and platforms also contains a list of articles.

有关更多详细信息,请查看我几个月前问的这个stackoverflow问题.

For more detailed information please look at this stackoverflow question that I asked a few months ago.

https://stackoverflow.com/a/40377383/5770147

问题在于如何创建文章以及如何实现文章与平台之间的N-N关系.

The question was on how to create an article and implement the N-N relationship between article and platform.

我有创建文章"和删除文章设置",以便列表也在平台中更新.

I have Creating article and Deleting the article setup so that the lists update in the platforms aswell.

如何实现对文章的编辑,以便可以更新链接到文章的平台?

How do I implement editing an article so I can update which platforms are linked to an article?

对于创建和编辑链接平台,我使用一个下拉菜单,可以在其中选择多个选项.可以在先前链接的问题中找到所需的代码.

For creating and editing the linked platforms I use a dropdown menu in which multiple options can be selected. The necessary code can be found in the question previously linked.

推荐答案

根据您提供的信息,我将建议从同一个基础开始的两种可能的方法:

Based on the information that you provided, I would recommend two possible approaches, starting from the same foundation:

使用两个集合(文章和平台),并且仅将对平台文档的引用存储在文章中定义的数组中 文件

Use two collections (articles and platforms) and store only a reference to platform documents in an array defined on article documents

在以下情况下,我会推荐这种方法:

I would recommend this approach if:

  • 您同时拥有两个文章文档以及 平台
  • 您希望能够独立管理两个实体,而 还在它们之间同步引用

  • You have a high cardinality of both article documents, as well as platforms
  • You want to be able to manage both entities independently, while also syncing references between them

// articles collection schema
{
"_id": ...,
"title": "I am an article",

...

"platforms": [ "platform_1", "platform_2", "platform_3" ],
...
}


// platforms collection schema    
{
"_id": "platform_1",
"name": "Platform 1",
"url": "http://right/here",
...
},

{
"_id": "platform_2",
"name": "Platform 2",
"url": "http://right/here",
...
},

{
"_id": "platform_3",
"name": "Platform 3",
"url": "http://right/here",
...
}

即使这种方法非常灵活,也要付出一定的代价-如果您同时需要商品数据和平台数据,则由于数据被分为两个不同的集合,因此您将不得不向您的MongoDB实例发起更多查询.

Even if this approach is quite flexible, it comes at a cost - if you require both article and platform data, you will have to fire more queries to your MongoDB instance, as the data is split in two different collections.

例如,在加载文章页面时,考虑到您还想显示platforms列表,则必须向articles collection发起查询,然后在platforms collection上触发搜索通过article documentplatform s数组的成员检索该文章发布到的所有平台实体.

For example, when loading an article page, considering that you also want to display a list of platforms, you would have to fire a query to the articles collection, and then also trigger a search on the platforms collection to retrieve all the platform entities to which that article is published via the members of the platforms array on the article document.

但是,如果只有少量经常访问的platform attributes子集在加载article document时需要可用,则可以增强articles collection上的platforms数组以存储这些属性. _id对平台文档的引用:

However, if you only have a small subset of frequently accessed platform attributes that you need to have available when loading an article document, you might enhance the platforms array on the articles collection to store those attributes in addition to the _id reference to the platform documents:

// enhanced articles collection schema  
{
"_id": ...,
"title": "I am an article",

...

"platforms": [
    {platform_id: "platform_1", name: "Platform 1"},
    {platform_id: "platform_2", name: "Platform 2"},
    {platform_id: "platform_3", name: "Platform 3"}
],

...

}

如果您经常检索以与文章特定数据一起显示的platform data attributes的更改不那么频繁,则此混合方法将是合适的.

This hybrid approach would be suitable if the platform data attributes that you frequently retrieve to display together with article specific data are not changing that often.

否则,您将必须将platforms collection中对platform document attributes进行的所有更新与作为文章文档平台数组一部分而跟踪的属性子集进行同步.

Otherwise, you will have to synchronize all the updates that are made to the platform document attributes in the platforms collection with the subset of attributes that you track as part of the platforms array for article documents.

关于各个平台的文章列表的管理,我不建议在两个集合中存储N对N引用,因为上述机制已经允许您通过使用查找查询来查询articles collection来提取文章列表platform document_id值:

Regarding the management of article lists for individual platforms, I wouldn't recommend storing N-to-N references in both collections, as the aforementioned mechanism already allows you to extract article lists by querying the articles collection using a find query with the _id value of the platform document:

Approach #1
db.articles.find({"platforms": "platform_1"});

Approach #2:
db.articles.find({"platforms.platform_id": "platform_1"});

已经介绍了两种不同的方法,我现在建议您分析应用程序的查询模式和性能阈值,并根据遇到的情况做出决策.

Having presented two different approaches, what I would recommend now is for you to analyze the query patterns and performance thresholds of your application and make a calculated decision based on the scenarios that you encounter.

这篇关于在mongodb中编辑子文档N-N关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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