Tag Archives: mongo

Case Insensitive search in MongoDB

Regex comes to rescue.

Pattern: /query/i

Caveat: Regex is a performance spoilsport when it comes to millions of records esp. because Regex ignores indexes.

Possible Options:

  1. Use regex for not-so-huge datasets
  2. Or go un-normalized, store the to-be-searched-attribute in lower case as another field. Fire the search query against lowercase version of attribute thus saving yourself the performance bottlenecks caused by regex.

Reference: http://stackoverflow.com/questions/9824010/mongoose-js-find-user-by-username-like-value

MongoExport | Date filtering

Export Mongo data queried on dates filter –
    1. Convert date value to numerical representation like so –

new Date(2009,1,25) * 1

2.  Use the mongoexport utility in the following format –

mongoexport —db dbName —collection collName —out /backups/bakName —fields “field1,f2,f3,f4,f5” -q “{‘\$and’: [{‘f2.f6’: ‘xyz’}, {‘f1’: ‘field1’}, {‘created_on’: {‘\$gte’: new Date(1359072000000)}}, {‘created_on’: {‘\$lte’: new Date(1359158400000)}}]}”

References:

http://stackoverflow.com/questions/13963699/mongoexport-json-assertion-10340-failure-parsing-json-string

http://stackoverflow.com/questions/5191186/how-do-i-dump-data-for-a-given-date