繁体   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