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!
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With