Using MongoDB With BackboneJS

So I ran into a small issue while working on a Rails 3.1 app which uses MongoDB as a backend and BackboneJS for bits and pieces of interactive ui. Changes in the Collections, i.e. addition or removal of Model objects to the Collection were not triggering appropriately binded View functions.

In MongoDB to identify a document uniquely we have “_id” attribute, which is similar to “id” column in relational database, which identifies a row uniquely.

And since by default, BackboneJS looks for “id” attribute to uniquely identify a Model object. It wasn’t able to know if a unique Model object had been added or removed from the Collection. The fix to the problem turns out to be pretty simple and is clearly mentioned in the comments of the BackboneJS library itself.

All you have to do is to explicitly set the idAttribute to “_id” in BackboneJS Model.

 idAttribute : '_id'

And everything should work fine after that as far as MongoDB with BackboneJS is concerned.


« First Post How Logical AND and OR Operators Actually Work in Javascript. »

Want articles and awesome free goodies sent directly to your inbox?

I won't spam you, promise! Unsubscribe at any time.


Enjoyed the article? Follow me on Twitter.