I'm working on a project that use MongoDB; and I would like to hear your opinion about a feature I'd like to implement. In paticular there are "Users" that reside in "Cities" where they offer "Services". I have created three Collections representing the three above mentioned entities: the User collection has a one-to-one reference with City and a one-to-many one with Service. I would like making a search function that search in the user collection and in referenced collections for a given string available. Therefor given the following two users, two cities and three services ... User
{
_id:"u1",
name:"Jhon",
City: ObjectId("c1"),
Services: [
ObjectId("s1"),
ObjectId("s2")
]
}
{
_id:"u2",
name:"Jack",
City: ObjectId("c2"),
Services: [
ObjectId("s2"),
ObjectId("s3")
]
}
City
{
_id:"c1",
name: "Rome"
}
{
_id:"c2",
name: "London"
}
Services
{
_id:"s1",
name: "Repair"
}
{
_id:"s2",
name: "Sell"
}
{
_id:"s3",
name: "Buy"
}
...and searching for the word "R", the result should be the u1 user (due to the R in "Rome" and "Repair"). Given that I cannot do joins, I was thinking making a mongo shell script that adds an additional field to the User collection with all the searcheable referenced strings. As in the following example
{
_id:"u1",
name:"Jhon",
City: ObjectId("c1"),
Services: [
ObjectId("s1"),
ObjectId("s2")
],
"idx":{
city: "Rome",
services:["Repair","Sell"]
}
}
Finally the question(s)... Do you think is it a good choice? And Can you propose an alternative solution (or share a link about that, i didn't find nothing usefull)? And how would you mantain that field constantly updated; for instance, What about if the referenced city name or the services offered by a user change?