regex - How to search comma separated data in mongodb -


i have movie database different fields. genre field contains comma separated string :

{genre: 'action, adventure, sci-fi'} 

i know can use regular expression find matches. tried:

{'genre': {'$in': genre}} 

the problem running time. take lot of time return query result. database has 300k documents , have done normal indexing on 'genre' field.

would use map-reduce create separate collection stores genre array values coming split comma separated string, can run map-reduce job , administer queries on output collection.

for example, i've created sample documents foo collection:

db.foo.insert([     {genre: 'action, adventure, sci-fi'},     {genre: 'thriller, romantic'},     {genre: 'comedy, action'} ]) 

the following map/reduce operation produce collection can apply performant queries:

map = function() {     var array = this.genre.split(/\s*,\s*/);     emit(this._id, array); }  reduce = function(key, values) {     return values; }  result = db.runcommand({     "mapreduce" : "foo",      "map" : map,     "reduce" : reduce,     "out" : "foo_result" }); 

querying straightforward, leveraging queries multi-key index on value field:

db.foo_result.createindex({"value": 1});  var genre = ['action', 'adventure']; db.foo_result.find({'value': {'$in': genre}}) 

output:

/* 0 */ {     "_id" : objectid("55842af93cab061ff5c618ce"),     "value" : [          "action",          "adventure",          "sci-fi"     ] }  /* 1 */ {     "_id" : objectid("55842af93cab061ff5c618d0"),     "value" : [          "comedy",          "action"     ] } 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -