0

I am performing aggregation command in mongodb php application like below lines of code.

<?php

 $query =  array('$or' => array(
    array('employeeList'=>array('$exists' => false)),
    array('employeeList'=>array('$eq' => null)),
    array('employeeList'=>array('$eq' => ",")),
    array('employeeList'=>array('$eq' => ""))
));

$pipeline = array(
    array(
        '$match' => $query
    ),
    array(
        '$lookup' => array(
            'from' => 'userTbl',
            'localField' => 'user_id',
            'foreignField' => 'uid',
            'as' => 'userdetails'
        )
    ),
);

$output = $this->db->broadcastTbl->aggregate($pipeline);
$result =array();
array_push($result, $output);

Now the output is displayed as

[{"waitedMS":0,"result":[{"_id":{"$id":"58d7a6561d78597411000029"},"broadcast_id":35,"studentList":"","employeeList":"999","mailTitle":"hello","broadcastMessage":"how","emailSent":"0","userdetails":[]},{"_id":....
...
"ok":1}] 

I want to remove "waitedMS":0. "result":, and "ok":1 from the json. The output should be like

[{"_id":{"$id":"58d7a6561d78597411000029"},"broadcast_id":35,"studentList":"","employeeList":"999","mailTitle":"hello","broadcastMessage":"how","emailSent":"0","userdetails":[]}, ...
]

Please help me !!!

miken32
  • 42,008
  • 16
  • 111
  • 154
Nida
  • 1,672
  • 3
  • 35
  • 68
  • I don't see any output here, but wouldn't that just be `$output["result"]` then? – miken32 Apr 06 '17 at 05:40
  • you are absolutely right but it is displaying now as [[ .... ]] and i want [...] – Nida Apr 06 '17 at 05:45
  • Possible duplicate of [How do I extract data from JSON with PHP?](http://stackoverflow.com/questions/29308898/how-do-i-extract-data-from-json-with-php) – miken32 Apr 06 '17 at 05:48

1 Answers1

1

All you need to do is access the "result" item. Don't push it onto another array.

<?php

 $query =  array('$or' => array(
    array('employeeList'=>array('$exists' => false)),
    array('employeeList'=>array('$eq' => null)),
    array('employeeList'=>array('$eq' => ",")),
    array('employeeList'=>array('$eq' => ""))
));

$pipeline = array(
    array(
        '$match' => $query
    ),
    array(
        '$lookup' => array(
            'from' => 'userTbl',
            'localField' => 'user_id',
            'foreignField' => 'uid',
            'as' => 'userdetails'
        )
    ),
);

$output = $this->db->broadcastTbl->aggregate($pipeline);

$result = $output["result"];
miken32
  • 42,008
  • 16
  • 111
  • 154