根據您提供的,我會建議兩種可能的方法,從相同的基礎上開始的信息:
使用兩個集合(文章和平臺),並只存儲在陣列平臺文檔參考
定義,如果我會推薦這種方法這兩種物品的文件,以及 平臺
你想成爲能夠獨立管理兩個實體的EA高基數,而 也同步
// 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實例發送更多查詢,因爲數據分爲兩個不同的集合。
例如,加載文章頁面的時候,考慮到你還想要顯示的platforms
列表,你將不得不解僱查詢到articles collection
,然後又觸發了platforms collection
搜索檢索所有平臺通過article document
上的platform
的數組成員發佈該文章的實體。
但是,如果你只有一小部分的頻繁訪問platform attributes
,你需要有可用的時候加載article document
,你可能會加強對articles collection
的platforms
陣列除了這些屬性存儲到_id
參考平臺文件:
// 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
您經常檢索到條具體數據同時顯示並沒有改變日經常。
否則,您必須將platforms collection
中的platform document attributes
的所有更新與您作爲文檔文檔的平臺數組的一部分跟蹤的屬性的子集進行同步。
關於管理單個平臺的文章列表,我不會建議在兩個集合中都存儲N對N引用,因爲上述機制已允許您通過使用查詢查詢articles collection
來提取文章列表,該_id
值platform document
的:
Approach #1
db.articles.find({"platforms": "platform_1"});
Approach #2:
db.articles.find({"platforms.platform_id": "platform_1"});
已經提出了兩種不同的方法,我現在會建議爲你分析查詢模式和應用程序的性能閾值,並根據場景的計算決定,你遭遇。
@chridam看到以前的解決方案來自你,你有什麼想法如何實現這 –