mongodb - Mongo regex to prioritize anchor at the beginning and fall back to substring match in ONE query -


assume have collection complete set of records 'aaaa' 'zzzz':

{text: 'aaaa'} {text: 'aaab'} ... {text: 'zzzy'} {text: 'zzzz'} 

i want provide n autocomplete matches against it. thus, m should return maaa, maab etc. before returning aaam or aabm. can done in one query?

if search {text: {$regex: 'm'}}, whatever stored in $natural order returned first. sorting text doesn't help. regex anchoring target string @ beginning, {text: {$regex: '^m|m'}}, doesn't either, ^m fails , .*m match instead. i've tried $or-ing two, still results matching m returned first.

here's the same question sql.

i suspect i'll have run first query {text: {$regex: '^m'}} , if fewer n results return, append results second query m anywhere in string, {text: {$regex: 'm'}}.

is there faster way prioritize matches closer start of string, or @ least start @ ^?


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -