Oddly enough I had a problem at work that I also found in my own programming.
Just gonna dive straight into it.
Mongo and Dynamic Languages
Mongo is a pretty neat “NoSQL” database that uses a JSON like format (called BSON) for storing information. If you have never heard of Mongo, you should definitely check it out.
Mongo is fairly flexible and stores what you give it, in the type that you give it.
If you give it “5” (in quotes) it will store it as the string “5”.
If you give it 5 (no quotes) it will store it as the number 5.
This usually is not a problem unless you meant to give it 5 and gave it “5” instead. Because when you ask for it back and it hands a nice little “5” to your dynamic language (when you think it is a 5) you may get something you were not anticipating.
In javascript, when you add 1 to your “5” you get “51” instead of 6. And when you subtract 1 from you “5” you get NaN (Not a Number) which is so useful.
Mongo Lookups
Another interesting behavior that arises is when you ask Mongo for that nice 5 you stored it fails to find it. Because 5 the number is not “5” the string. Funny how that works.
This can make some code look like it is not working when in fact you simply have inconsistent data storage and retrieval patterns.
Moral of the Story
Be careful in dynamic languages when using a storage system like Mongo that doesn’t care what you give it.
Use your type casting functions like parseInt to insert and retrieve data, or use a database that cares what kind of data is going in and out.