【发布时间】:2018-09-17 13:24:31
【问题描述】:
我对 MongoDB 中的多对多关系实现有一个特定的问题。
我收藏了歌曲和艺术家(百万文档)。在这里可以唱这首歌 by Many Artists 一个艺术家可以唱很多歌。所以我跟着 两个集合中的文档引用方法。像这样……
1. 歌曲合集:-
{
_id:ObjectId("dge547567hheheasfw3454dfg"),
title:"xyz",
artists:[ObjectId("xfvdg464654"), ...] //many artists // artists ids
}
2. 艺术家收藏:-
{
_id:ObjectId("dge547567hheheasfw3454dfg"),
title:"xyz",
songs:[ObjectId("xfvdg464654"), ...] //many songs // songs Ids
}
但这里的问题是,在删除艺术家时,我必须从包含艺术家的歌曲的所有文档中的艺术家数组中删除艺术家,反之亦然。这会导致 原子性问题。 这里如何保证原子性?
其次,当数据库增长并由艺术家演唱歌曲时 将增加因此导致文档集的增长和文档大小可以达到 16MB 或更大(MAX DOC SIZE)。
那么在这种情况下可以做些什么呢?
【问题讨论】:
-
But here the problem is while doing CRUD Operation on one collection I have to do CRUD operation on other collection. which can cause the problem of Atomicity-> MongoDB 将这项工作留给必须明确确保数据一致性的程序员。 MongoDB 确保document级别的原子性。Secondly when the database will grow and songs are sung by the artist will increase thus resulting document growth of both collection and document size can reach to 16MB or greater-> 你没有存储整个歌曲/艺术家收藏,只是一个 ID,所以理想情况下这里没有问题..
标签: mongodb mongoose schema entity-relationship