Reputation: 771
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!
Upvotes: 4
Views: 8040
Reputation: 101
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
Upvotes: 3
Reputation: 2165
Ideally it should be stored within one collection as a nested sub-document OR an array of places. NoSql should be used for such requirements with nested data objects/arrays rather than traditional RDBMS relationships.
Upvotes: 0