Filtering on multiple fields in MongoDB
This thread got me thinking about filtering on multiple fields in a MongoDB query. The author of the original question attempted to use the $and operator which was introduced in MongoDB 2.0. But that’s not needed for simple queries that filter on multiple fields. I created a simple report to show the idea.
The report is available here if you want to play with it. Here’s the sample data I used:
db.shiraz.save( { "ServiceType" : "sms", "ServiceHealthState" : "ok", "DocumentID" : "11" } )
db.shiraz.save( { "ServiceType" : "sms", "ServiceHealthState" : "ok", "DocumentID" : "12" } )
db.shiraz.save( { "ServiceType" : "sms", "ServiceHealthState" : "ko", "DocumentID" : "13" } )
db.shiraz.save( { "ServiceType" : "jms", "ServiceHealthState" : "critical", "DocumentID" : "21" } )
db.shiraz.save( { "ServiceType" : "jms", "ServiceHealthState" : "critical", "DocumentID" : "22" } )
If you haven’t used (or heard of!) the Jaspersoft connector for MongoDB, then start with this article.
Advertisement
