find - MongoDB: $all with empty array... -


i'm running query on collection documents these.

{"name": "daniel", "tags": ["person", "owner", "father"]}, {"name": "jane", "tags": ["person", "owner", "mother"]}, {"name": "jimmy", "tags": ["person", "owner", "son"]} 

now, if want find documents matching tags person , owner this

var match = ['person', 'owner']; model.find({'tags': {$all: match}}); 

now need following:

  1. when match has values, return document matching (this done)
  2. when match empty [ ], return documents.

what's efficient way in single query?

thanks in advance,

d

i suggest add condition in application layer, , keep query gets executed on server simple.

  • if match array has 1 or more elements, add query field query condition, else execute empty condition return documents.

snippet:

var match = ['person', 'owner']; var condition = {}; if(match.length > 0){    condition["tags"] = {$all:match}; } db.model.find(condition); 

having said this, $all operator, not possible achieve both conditions in single query.

the $all operator selects documents value of field array contains specified elements.


Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -