MongoDB data modeling issue

39 views Asked by At

I'm have put together a schema in MongoDB to model my domain but I'm not sure what's the best approach considering what I will need to search on (premise: it's my first "big" project with a document database and I have quite a strong RDBMS background).

Here's an example document:

{
  id: 1,
  name: "Building A",
  description: "blah blah",
  geoLocation: [long, lat],
  flats: [
           {
              id: 1,
              name: "Flat A",
              squareFeet: 800
            },
           {
              id: 2,
              name: "Flat B",
              squareFeet: 900
           }
         ] 
}

I then have a search functionality which is supposed to search for flats. Assuming that that document is my whole DB, a blank search should return 2 results (the 2 flats).

This makes me think that I should change my schema to have a document per flat. The main issue I have with that is that the search will mainly be a location search and the location information lives with the Building and not with the flats. At the same time I need to be able to filter on square feet which is obviously a Flat level field.

Should I just create a document per Flat and duplicate the building information ? Problem with that obviously being updates at the Building level which can end up updating a lot of documents (1 update per flat).

I then thought about having to separate collections and create a relationship between the 2 (RDBMS style). Is that the best approach on a document database though ? If so, will that mean that after I search I have to query again and retrieve the buildings details ? Also, how will I handle the location search if the location information will be on the buildings document ?

I hope I did explain it decently well.

Any help ?

Thanks

0

There are 0 answers