关于"one to many"关系的NoSQL数据建模



晚上好,

我正在尝试以MongoDB为后端开发小型数据模型。这是我第一次使用NOSQL数据库和数据模型开发。

最大的想法是:可以完成很多服务,并且每个服务在不同的地方都可以使用。例如,让我们想一想一个显示在哪里吃饭(服务)的网站,您可以在多个地方做到这一点,因此许多地方(地方)有1个服务(饮食)。

我不知道该如何建模,有什么想法?

SERVICE {
_id: ObjectId,
Name: String,
Code: String
} // Data model for the Service Collection?
PLACES {
_id: ObjectId,
NameOfPlace: String,
Service1: String,
Service2: String,
...
Servicen: String
} /* This way, by using the flexibility of MongoDB, 
updating the document each time with some new (key, value).
Doesnt seem elegant nor efficient.*/
PLACES {
_id: ObjectId,
NameOfPlace: String,
Services: {
           NameOfService1: refID SERVICE COLLECTION,
           NameOfService2: refID SERVICE COLLECTION..
          }
} // Maybe this way is better? No idea how to do those tho'

您可以向我解释的更好的解决方案吗?谢谢大家!

您应该考虑使用引用对它们进行建模。基本上,一个地方有一组serivce的ID

SERVICE {
_id: ObjectId,
Name: String,
Code: String
}
PLACES {
_id: ObjectId,
NameOfPlace: String,
Services: [Service1_id, Service2_id, ...]
}

您可以阅读以下链接以进行实施详细信息

mongoDB-数据建模简介

mongoDB-与参考的模型一对多关系

理想情况下,它应该存储在一个集合中,作为嵌套的子文档或一系列位置。NOSQL应用于使用嵌套数据对象/数组而不是传统的RDBMS关系来用于此类要求。

最新更新