6

When converting a MongoCursor to PHP I use this script. Which was presented here StackOverflowSO

using the upper method, the structure is same but _id is whereas using the lower script which yields the below included result.

Unfortunately, this results in the actual object being embedded into an array with the _id from Mongo. Like this :

`4eefa79d76d6fd8b50000007 =             {
            "_id" =                 {
                "$id" = 4eefa79d76d6fd8b50000007;
            };
            longText = "Error Description";
            nCode = dee29fd7e15ce4ab2d3f7dfa7c5d8fc44b27501ad00908771128c920ef276154;
            nStatus = Process;
            nText = "E12345";
            nVType = Type1;
            pId =                 {
                "$id" = 4eefa79676d6fd8b50000003;
            };
            pushDate = "2011-12-20+06%3A07%3A41";
            updateFlag = 1;
        };`

Since I am passing this object to another service for processing the _id is not known.

How can I convince the PHP Driver to parse the object properly?

3
  • i don't understand your question
    – Petrogad
    Dec 21, 2011 at 13:45
  • The problem is that id is getting nested, but we need them to post their code. Please post your code user1094824. Dec 21, 2011 at 16:09
  • '_id' containing '$id' is actually to be expected, as his document contains a MongoId object (mongo generated key, returned by php as MongoId class/object), which is represented with '$id'.
    – Jon L.
    Apr 9, 2012 at 18:08

2 Answers 2

5

Basically what I did was this.

return json_encode(iterator_to_array($cursor));

But this created the aforementioned object which is not what I needed.

I solved it in this way.

 $i=0;

   foreach($cursor as $item){
       $return[$i] = array(
           '_id'=>$item['_id'],
           'nCode'=>$item['nCode'],
           'pId'=>$item['pId'],
           'nText'=>$item['nText'],
           'longText'=>$item['longText'],
           'nStatus'=>$item['nStatus'],
           'nVType'=>$item['nVType'],
           'pushDate'=>$item['pushDate'],
           'updateFlag'=>$item['updateFlag'],
           'counter' => $i
                    );
       $i++;
   }

return json_encode($return);

2

If you result is big in order to save RAM you could try this more efficient method:

function outIterator($iterator, $resultName='results')
{
    // Efficient MongoCursor Iterator to JSON
    // instead of encoding the whole result array to json
    // process each item individually
    // in order to save memory by not copying the data multiple times

    //Start Json Output
    header('Content-Type: application/json');
    echo '{' . $resultName . ': ['

    //Output each item as json if there are results in the iterator     
    if ($iterator->hasNext()){
        foreach ($iterator as $item)
        {   
            echo json_encode ($fixeditem);
            if ($iterator->hasNext()) echo ', ';
        }
    }

    //end Json output
    echo  ']}';
}

$results = $db->collection->find();
outIterator($results);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.