Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NoSQL Data Modeling about "one to many" relationships

Good evening,

I'm trying to develop a small data model with MongoDB as backend. It's my first time developing with NoSQL databases and data models.

The big idea is this: there are a bunch of SERVICES that can be done and each SERVICE is doable in different places. As an example let's think of a website that shows where to eat (the Service), there are multiple places where you can do that, so there's 1 Service (EAT) for many places (PLACES).

I don't know how to model this, any ideas?

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'

Any better solutions that you can explain to me? Thanks everyone!

like image 508
404answernotfound Avatar asked Oct 15 '25 23:10

404answernotfound


1 Answers

You should consider modeling them using references. Basically, a Place has a collection of Serivce's Ids

SERVICE {
_id: ObjectId,
Name: String,
Code: String
}

PLACES {
_id: ObjectId,
NameOfPlace: String,
Services: [Service1_id, Service2_id, ...]

}

You may read the following links for implementation details

MongoDB - Data Modeling introduction

MongoDB - Model One-to-many relationship with reference

like image 112
tcthanh Avatar answered Oct 17 '25 12:10

tcthanh