mongodb - Compare additional fields only if multiple matches? -
i have unique documents indexed non-unique keys. makes document unique, combination of multiple keys within document. example:
{ first: 'john', last: 'foo' } { first: 'henry', last: 'bar' } { first: 'frank', last: 'foo' } { first: 'john', last: 'bar' }
so, based on illustration above: if wanted query first name of frank
, 1 result. ideally, since have 1 result, wouldn't need compare lastly name our query. however, if query name john
, 2 results, would need compare secondary argument.
how style of query achieved in mongo? goal save needless compares if there single match begin with.
note aware style of query doesn't guarantee right document. assumes primary, , each subsequent field match, "good enough" verify identity of document, if 1 document matched. though if there other less obvious reasons why method should not used, means discuss :)
i wouldn't worry @ all, if have index on this. compound index on first, lastly scan index elements start "first". if that's 1 document, stops. if needs match "last", scan parts of index.
mongodb
No comments:
Post a Comment