0

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?

  • 1
    In my opinion your model is wrong. Cities are so static that they should not be a reference to a City collection, but just a raw list. That's how you would make Mongo excel compared to relational databases. The way you're doing it, it would be better to use a relational database. Probably the same case for your services. You should read about how to model NoSQL and why you would model these differently. The concept is different. Also consider using ElasticSearch for complex searches. – Erik Duindam Feb 22 '15 at 11:09
  • Hi Erik. I also think that my model is wrong but i've looked in mongodb docs and i didn't find some useful resource that explain how to model for that kind of search. What if i need to reference entities that aren't so static?. I will take a look at ElasticSearch as you suggested. Thank you for your comment – Gianni Ciccarelli Feb 22 '15 at 11:35
  • To evaluate your model we need to know what kinds of operations you will do commonly - what reads, what writes? – wdberkeley Feb 23 '15 at 17:29
  • I think the number of reads and writes is balanced; in fact, the registered users can change their city and their services very frequently. At the same time, users can search other users by city or by service. In other words, my question is: could it be ok putting an extra field exposing all searchable strings given that, for any reason, i must model the user collection with referenced documents while searching users by fields in those referenced documents? If yes, how keeping that field constantly updated to changes in a transparent way? Thanks. – Gianni Ciccarelli Feb 23 '15 at 22:36

0 Answers0