Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you handle relations in a NoSQL database?

With a standard RDMS I can find relations by using primary keys and foreign keys. If I want recent comments then I just order by a datetime. If I want all the comments by a user, then I fetch where a comment belongs to that user.

In other words, I can use indexes to filter results. Not just the primary key.

However, with the document and key-value NoSQL I can't figure out how I could use them for much more than a text dump. The only thing you can do is fetch the value by an ID.

I need some examples of how you model data in NoSQL when you can no longer use indexes or filters. How do you sort and search data?

like image 249
Xeoncross Avatar asked Feb 14 '11 17:02

Xeoncross


1 Answers

If you need secondary indexes like you're describing, then you can't just use any non-relational database. BigTable databases like Cassandra (and probably others) allow for secondary indexes.

If you need to search for things within a Key-Value store based on the values then you'll need to get creative. You could:
1) Create your own keys that point at the original keys and then maintain those pairs on new inserts, updates, and deletes of the original pairs.
2) Just look at every value, brute force, off-line, once a day and save the answer somewhere. Clearly this won't work if you need the new data right away.

Sorting the data will probably need to be done on the application layer or with custom sorted sets if you use technique (1) and Redis.

like image 112
Andrew McKnight Avatar answered Nov 08 '22 21:11

Andrew McKnight