MongoDB PHP - findOne, using query to filter results not working for a specific item -
this mongo collection i'm trying test on:
{"_id":{"$id":"54d5002adc533bf41000002c"},"tasks":[{"taskid":1,"taskname":"task 1 name here","subtasks":[1],"coords":{"gantt":{"x":10,"y":30},"pert":{"x":90,"y":100}}},{"taskid":2,"taskname":"task 2 name here","participators":[1,2],"startdate":"5-12-2014","enddate":"5-21-2014"},{"taskid":3,"taskname":"task 3 name here","subtasks":[3],"participators":[1]}],"participators":[{"participatorid":1,"participatorname":"participator 1 name here"},{"participatorid":2,"participatorname":"participator 2 name here"}]}
i'm trying filter data based on id , return set of tasks, filtering using taskid.
here's code i'm using:
$cursor = $this->mongo->findone(['_id' => $mongoid, 'tasks.taskid' => 2], ['_id' => false, 'tasks.taskid' => true, 'tasks.coords.gantt' => true]);
this should return taskid 2's data; can see i'm trying filter displaying taskid of 2. but instead returns:
{"tasks":[{"taskid":1,"coords":{"gantt":{"x":10,"y":30}}},{"taskid":2},{"taskid":3}]}
i don't know how i'm meant filter results specified taskid's data returned.
thank you.
-- update -- fixed using following code:
$cursor = $this->mongo->findone(['_id' => $mongoid], ['_id' => false, 'tasks' => ['$elemmatch' => ['taskid' => 1]], 'tasks.coords.gantt' => true]);
if want find element in array need use $elemmatch projection.
Comments
Post a Comment