简体   繁体   English

不确定如何在 MongoDB 中建立 model 多对多关系

[英]Unsure how to model many-to-many relationship in MongoDB

I'm new to MongoDB and I'm trying to model a Many to Many relationship into at least 2 collections (I need two collections for the project).我是 MongoDB 的新手,我正在尝试将 model 与至少 2 个 collections 的多对多关系(我需要两个 Z0B9ABFE67CC931FC16 项目的 Z0B9ABFE67CC931FCF16)。 What I'm having is a collection of universities, faculties and specializations, and another collection for students and their gradebook (this was the middle entity between specializations and students in SQL, not sure if it's needed in Mongo anymore).我所拥有的是大学、学院和专业的集合,以及学生和他们的成绩簿的另一个集合(这是 SQL 中专业和学生之间的中间实体,不确定 Mongo 是否需要它)。 I tried to use This as an inspiration but it limits me as I can only search students by university id (I want for example to search students from a certain specialization or a certain faculty).我试图以此为灵感,但它限制了我,因为我只能通过大学 ID 搜索学生(例如,我想搜索来自某个专业或某个学院的学生)。 I could put every row from university, faculty and specialization in the student collection and vice versa but I really don't think it's ideal.我可以将大学、教师和专业的每一行都放在学生收藏中,反之亦然,但我真的不认为这是理想的。 Here's what I have so far:这是我到目前为止所拥有的:

db.students.insertOne({_id:1, firstname: 'John', lastname: 'Silas', ethnicity:'english', civilstatus:'single', residence:'London', email:'johnSilas@gmail.com', gradebook:[{ year:2018, registrationyear:2017, formofeducation:'traditional'}], universities:[1]}) 
db.universities.insertOne({_id:1, name:'University of London', city:'London', adress:'whatever', phone: 'whatever', email: 'whatever@gmail.com', faculty:[{name: 'Law', adress:'whatever', phone: 'whatever', email: 'whatever@gmail.com'}], specialization:[{name:'criminal rights', yearlytax:5000, duration: 3, level:'bachelordegree', language:'english'}], students: [1,2]}) 

I'm sorry if I don't understand basic noSQL concepts, I am new to it.如果我不了解基本的 noSQL 概念,我很抱歉,我是新手。 Thanks in advance.提前致谢。

Basic patterns for many to many association between A and B: A和B之间多对多关联的基本模式:

Inline references内联引用

  • On A, store the list of B ids in a field like b_ids在 A 上,将 B id 的列表存储在b_ids类的字段中
  • On B, store the list of A ids in a field like a_ids在 B 上,将 A id 的列表存储在a_ids类的字段中

This requires two writes whenever an association is created or destroyed, but requires either zero or one joins at query time to traverse the association (if you just want the id of Bs for a given A and you have the A already no further queries are needed).每当创建或销毁关联时,这需要两次写入,但在查询时需要零个或一个连接来遍历关联(如果您只想要给定 A 的 Bs 的 id 并且您已经拥有 A 不需要进一步的查询)。

Join model加入model

Create a model C which has two fields: a_id and b_id .创建一个 model C ,它有两个字段: a_idb_id Each association is represented by a single instance of C.每个关联都由 C 的单个实例表示。

This requires one write whenever an association is created or destroyed, but requires joins on all queries involving association (potentially two joins per query).每当创建或销毁关联时,这需要一次写入,但需要对所有涉及关联的查询进行连接(每个查询可能有两个连接)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM