如何在MongoDB中表示多对多或多对一关系?

Saq*_*Ali 5 mongodb

我正在学习MongoDB,我有一个问题:你如何代表多对多或多对一的关系?在标准的SQL DB中,它很简单:

Parent Table has fields ID (primary key) and Name.
Child Table has fields ID (primary key) and Name
Parent-Child-Relationship Table has fields ID (primary key), ParentID and ChildID

insert into table Parent (ID, Name) values (1, "Bob");
insert into table Child (ID, Name) values (1, "Mahmoud");
insert into table Parent-Child-Relationship (ID, ParentID, ChildID) values (1,1,1);
Run Code Online (Sandbox Code Playgroud)

但我还没想出如何在MongoDB中做到这一点.我可以:

db.parent.save({name: "Bob", children: ["Mahmoud"]});
Run Code Online (Sandbox Code Playgroud)

但那我怎么能为Mahmoud创造另一个父母(说"玛丽")?

我错过了一些明显的东西吗 请帮忙.我是NoSQL技术的完全新手.

Jon*_*and 1

简短的回答是你不知道。

10Gen 会告诉您的答案是使用作为父文档的单个文档和代表子文档的子文档。

但是,不要这样做,因为 Mongo 中的子文档查询有限且速度较慢。

每个人最终要做的就是将父 ID 存储在子 ID 上,并在应用程序级别执行多个查询/连接。