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:
- when match has values, return document matching (this done)
- 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
matcharray has 1 or more elements, add query field querycondition, 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
Post a Comment