As mongodb does not support join and I have the need to search several collections of businesses, services, and users, I have arrived at a solution but I need to be validated and / or improved.
The flow of the scheme would look like this:
Business = Schema({
name:String
services:[{
type:ObjectId,
ref:'Services'
}],
specialist:[{
type:ObjectId,
ref:'User'
}]
})
User = Schema({
full_name:String
businesses:[{
_id:{
type:ObjectId,
ref:'Business'
},
name:String,
role:String,
is_owner:Boolean
}]
})
Service = Schema({
name:String,
specialist:[{
type:ObjectId,
ref:'User'
}]
})
Search = Schema({
text:{
type:String,
index:'text'
}
business:{
_id:{
type:ObjectId,
ref:'Business'
},
name:String
},
services:[{
_id:{
type:ObjectId,
ref:'Service'
},
name:
}],
specialist:[{
_id:{
type:ObjectId,
ref:'User'
},
full_name:
}]
}
})
Businesses offer services, services are carried out by specialists, and each time a business adds a specialist, the user will have a new business in their data with a specific role.
The idea is that when you create a business, you create a document in Search, and when the business believes services assign them to specialists, these are added to the Search collection. In each modification of that document the text field will be updated with a concatenation of the name of the business, services and specialists, with this they would have a single collection where to make the indexed searches of text. Any field you look for will result in business.
Who has suggestions, both in the model and in the solution of the common collection to search.