簡體   English   中英

我是否應該使用大量 collections 來幫助構建 MongoDB 中的數據?

[英]Should I use lots of collections to help structure my data in MongoDB?

我正在使用 NodeJS 制作服務器同步日記應用程序,並使用 MongoDB。 我在 MySQL 中有我所有的超級關系數據。 但是對於用戶日常回憶錄我將使用 Mongo,因為正如您可能已經意識到的那樣,會有大量的筆記/日常日記,我想學習 MongoDB,對於許多非關系型的人來說應該會更好數據。

我已經學會了如何創建數據庫並做所有事情,但是所有教程都沒有涵蓋的東西是最重要的,我如何構建我的數據?

下面我有幾個我的想法的例子,由於我對 Mongo 非常缺乏經驗,我想要一些建議,關於哪個選項將是最好的性能明智。

提前感謝您的時間,以及任何幫助!

示例 1:我的數據庫有一個名為“Days”的巨大集合,該集合的每個條目如下所示:(對不起,但無論我怎么想,這聽起來都是性能最差的選項,正如我所說的那樣對 Mongo 沒有經驗,可能是錯誤的。)

{
userID: 902, //This user ID will be fetched from MySQL when authenticating users request. From what ive read, I need to run a command similar to this: “db.posts.createIndex( { author_name : 1 } )”, on this collection to somehow optimize performance?

//What day? No, I wont use Date for this, because then id have to turn my JSON Query data to Date before querying (Maybe I wouldnt have to, as Mongo may store it as string anyway). BUT, I am not sure whether i should use 3 separate Integer fields, or one string field. Which would be faster? (EDIT: I know three separate fields with int will be WAY faster, as my application also has to query data for one month, etc. MAYBE Im wrong, and this is bad practice, let me know.)

day: 12,

month: 5,

year: 2018,

//Actual stored data:

dayTitle: “Lame day at home..”,

dayDescription: “Installed arch..”,

hugeLoadOfIndividualSmallNotesForThisDayWithTimeStamps: [

    { data: “Woke up, start now”, time: “9:44”,

    { data: “Finally figured out what fdisk is”, time: “21:29” } } …

]
}

示例 2:我的數據庫為每個用戶都有一個由他們的用戶 ID 命名的集合(這聽起來非常好,對我來說很有條理,按照我的常識,這聽起來像是性能最好的一個,但是從我用谷歌搜索的結果來看,人們說這不會很好,這正是我在這里問的原因),並且該集合的每個條目看起來像這樣:

{
day: 12,

month: 5,

year: 2018,

dayTitle: “Lame day at home..”,

dayDescription: “Installed arch..”,

hugeLoadOfIndividualSmallNotesForThisDayWithTimeStamps: [

    { data: “Woke up, start now”, time: “9:44”,

    { data: “Finally figured out what fdisk is”, time: “21:29” } } …

]
}

示例 3:我的數據庫每天都有一個集合。 (這與示例 2 基本相同,但 collections 會更少。我非常不確定這是否會比選項 2 性能更好,而且這會因為天數變化等而更難實施),並且該集合的每個條目如下所示:

{
userID: 902,

dayTitle: “Lame day at home..”,

dayDescription: “Installed arch..”,

hugeLoadOfIndividualSmallNotesForThisDayWithTimeStamps: [

    { data: “Woke up, start now”, time: “9:44”,

    { data: “Finally figured out what fdisk is”, time: “21:29” } } …

]
}

如前所述,提前感謝各位!

對於您的情況,最好將所有內容放在一個集合中。 您建議的所有其他分解數據的方法看起來都可以通過在用戶 ID 和日期字段上構建索引來得到很好的服務。

我傾向於使用 collections 將同一項目中的數據集組合在一起,但具有不同的數據結構。

如果您將天或用戶分成不同的 collections,那將如何擴展? 如果你想查詢所有天的所有文本,如果你的應用已經使用了十年,你想連接到幾千個不同的 collections 嗎? 嘗試為不同的用戶體驗編寫一些測試用例,看看編寫查詢以獲取數據是多么容易。

TLDR:可能最好將事物放在一個集合中,並使用索引來整理事物。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM