如何在 MongoDB 中设计这样的方案?我认为没有外键!
最佳答案
How to design table like this in mongodb?
首先,澄清一些命名约定。 MongoDB 使用 collections
而不是 tables
。
I think there are no foreign keys!
采取以下模型:
student
{
_id: ObjectId(...),
name: 'Jane',
courses: [
{ course: 'bio101', mark: 85 },
{ course: 'chem101', mark: 89 }
]
}
course
{
_id: 'bio101',
name: 'Biology 101',
description: 'Introduction to biology'
}
很明显,简的类(class)列表指向了一些特定的类(class)。数据库不对系统应用任何约束(即:外键约束),因此没有“级联删除”或“级联更新”。但是,数据库确实包含正确的信息。
另外,MongoDB 有一个 DBRef standard这有助于标准化这些引用的创建。事实上,如果你看一下那个链接,它有一个类似的例子。
How can I solve this task?
需要明确的是,MongoDB 不是关系型的。没有标准的“正常形式”。您应该根据您存储的数据和您打算运行的查询为您的数据库建模。
https://stackoverflow.com/questions/6334048/