Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suitability of MongoDB for hierarchial type queries

I have a particular data manipulation requirement that I have worked out how to do in SQL Server and PostgreSQL. However, I'm not too happy with the speed, so I am investigating MongoDB.

The best way to describe the query is as follows. Picture the hierarchical data of the USA: Country, State, County, City. Let's say a particular vendor can service the whole of California. Another can perhaps service only Los Angeles. There are potentially hundreds of thousands of vendors and they all can service from some point(s) in this hierarchy down. I am not confusing this with Geo - I am using this to illustrate the need.

Using recursive queries, it is quite simple to get a list of all vendors who could service a particular user. If he were in say Pasadena, Los Angeles, California, we would walk up the hierarchy to get the applicable IDs, then query back down to find the vendors.

I know this can be optimized. Again, this is just a simple query example.

I know MongoDB is a document store. That suits other needs I have very well. The question is how well suited is it to the query type I describe? (I know it doesn't have joins - those are simulated).

I get that this is a "how long is a piece of string" question. I just want to know if anyone has any experience with MongoDB doing this sort of thing. It could take me quite some time to go from 0 to tested, and I'm looking to save time if MongoDB is not suited to this.

EXAMPLE

A local movie store "A" can supply Blu-Rays in Springfield. A chain store "B" with state-wide distribution can supply Blu-Rays to all of IL. And a download-on-demand store "C" can supply to all of the US.

If we wanted to get all applicable movie suppliers for Springfield, IL, the answer would be [A, B, C].

In other words, there are numerous vendors attached at differing levels on the hierarchy.

like image 266
IamIC Avatar asked Apr 08 '11 19:04

IamIC


People also ask

Which database is best for hierarchical data?

Relational Databases The advantages of relational over other types of databases include the use of SQL, ease of changing the structure of tables, greater consistency in data, and handling of many-to-many relationships.


1 Answers

I realize this question was asked nearly a year ago, but since then MongoDB has an officially supported solution for this problem, and I just used their solution. Refer to their documentation here: https://docs.mongodb.com/manual/tutorial/model-tree-structures-with-materialized-paths/

The concept relating closest to your question is named "partial path."

While it may feel a bit heavy to embed ancestor data; this approach is the most suitable way to solve your problem in MongoDB. The only pitfall to this, that I've experienced so far, is that if you're storing all of this in a single document you can hit the, as of this time, 16MB document size limit when working with enough data (although, I can only see this happening if you're using this structure to track user referrals [which could reach millions] rather than US cities [which is upwards of 26,000 according to the latest US Census]).


References:

http://www.mongodb.org/display/DOCS/Schema+Design

http://www.census.gov/geo/www/gazetteer/places2k.html


Modifications:

Replaced link: http://www.mongodb.org/display/DOCS/Trees+in+MongoDB

like image 176
Caleb Gray Avatar answered Nov 16 '22 04:11

Caleb Gray