Tuesday, May 10, 2011

Running complicate mongo query without map / reduce

Such query is actually provided in mongo documentation, but I find it useful. Many of my friends are afraid of using MongoDB. They know that MongoDB is faster, but they have no ideas how to do mongo administration and extract information they want.

Map/Reduce and javascript function can do almost everything as SQL database do. But when I say the "you can do this in Map/Reduce, no one find it comfortable. No one thinks "writing map/reduce" is programmer-friendly. When I see their faces, it feels like I am cheating them that Mongo DB is easy to use. But be frank ... Very often, a multi join-ed group by having SQL query looks simple.

So... here is one query that I think most programmer do find it readable and useful to query things in Mongo DB.

db.myCollection.find( { is_active: true, $where: "this.credits - this.debits < 0" } );

It is one of the server-side query you can do. You can pass in javascript function into the where part to do something complicated. In this case, it find all active record and compared the condition of two fields.

More examples in mongo db page.

Yes, there are lots of differences doing things in NoSQL from relational SQL DB, however, provided the capability, I do recommend every programmer to try out one of them, and I would suggest Mongo DB to be the first test bed. Quite mature, large community user base, many articles sharing how to transit from SQL ecology.

No comments:

Post a Comment