Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB tips, tricks and gotchas [closed]

Tags:

mongodb

From your experience, please share your favourite MongoDB tip such as:

  • What have you learnt the hard way?
  • What features might be unexpected, especially for newbies coming from an RDBMS background?
  • Which best practices are most important?
like image 614
Chris Fulstow Avatar asked Dec 23 '10 23:12

Chris Fulstow


2 Answers

I've used MongoDB (and tried MongoMapper and Mongoid adapters with Rails) for a few months now. I was previously deeply entrenched in SQL. Here are my observations:

  • The lack of a schema and migrations doesn't mean you get a free lunch. Your application code will have to carry the logic and constraints that SQL schemas take care of. You'll have to be a lot more disciplined to manually migrate data, e.g. when you rename an attribute, or else your application code has to deal with multiple names for the same thing (in this example). This could easily lead to a lot of junk in the database and/or obscure errors. MongoDB is great because you don't have to define a priori what the stuff is you're going to store, and you can put entire arrays and object hierarchies into any attribute. In some ways that ease of use comes with a lot less safety, and I think we're still evolving a lot of the best practices around how to avoid building up a lot of technical debt here.
  • No joins -- that means you kind of have to know your data access patterns (at least the ones that need to be scalable) when you design your collections.
  • No transactions -- this opens the risk of race conditions if the same records in a collections are updatable from concurrent requests. For single-collection updates there are atomic operations, and these are particularly well supported and used by Mongoid (but not by MongoMapper).
  • 16MB record limit for any record. Unlike text fields in SQL which can take arbitrary-size content, MongoDB has a hard limit of 16MB for any record. This is worth a consideration as you're planning to store, e.g. large arrays, object hierarchies, etc.
like image 143
Wolfram Arnold Avatar answered Oct 22 '22 11:10

Wolfram Arnold


This article on MongoDB gotchas is a bit newer than the one linked above. http://rsmith.co/2012/11/05/mongodb-gotchas-and-how-to-avoid-them/

The related discussion is on HN: https://news.ycombinator.com/item?id=4745067

like image 32
herrjeh42 Avatar answered Oct 22 '22 11:10

herrjeh42