In MongoDB how to search in arrays for sequences and complex searches -



In MongoDB how to search in arrays for sequences and complex searches -

i want able search in documents subsequences.

for example

{ "name": "doc1", "sequence": ["a", "b", "d", "g", "k"] } { "name": "doc2", "sequence": ["c", "a", "b", "m", "d"] }

i want match multiple items in order. queries example:

return documents have sequence ["a","b"]. (returns doc1 , doc2) return documents have "a" , after 3 positions "d" (return doc2) return documents have sequence ["b","d","(whatever)", "k"] (return doc1)

i not sure can mongodb. solution save sequences strings instead of arrays , utilize regular expressions (but don't much solution).

if can't in mongodb, there nosql engine or whatever engine supports this?

i think not possible in mongodb.

first of not easy get particual element, can $elementmatch, far see there no way values of neighbors.

i suggest utilize strings. tried short perl illustration , works well.

#!/usr/local/bin/perl utilize strict; utilize warnings; utilize mongodb; utilize data::dumper; $client = mongodb::connection->new(host => 'localhost', port => 27017); $database = $client->get_database('oho'); $documents = $database->get_collection('documents'); $documents->remove(); $doc1 = { "name" => "doc1", "sequence" => ["abdgk"] }; $doc2 = { "name" => "doc2", "sequence" => ["cabmd"] }; $documents->insert($doc1); $documents->insert($doc2); @case1 = $documents->find( { "sequence" => qr/ab/i } )->all(); print "case 1:" . dumper \@case1; @case2 = $documents->find( { "sequence" => qr/a..d/i } )->all(); print "case 2:" . dumper \@case2; @case3 = $documents->find( { "sequence" => qr/bd.*k/i } )->all(); print "case 3:" . dumper \@case3;

arrays mongodb search nosql

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -