正确解析 Mongo 游标到 PHP

发布于 2024-12-21 23:34:42 字数 955 浏览 1 评论 0原文

将 MongoCursor 转换为 PHP 时,我使用此脚本。此处介绍的 StackOverflowSO

使用上面的方法,结构相同,但 _id 是,而使用下面的脚本会产生以下包含的结果。

不幸的是,这会导致实际对象被嵌入到带有 Mongo 的 _id 的数组中。像这样:

`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;
        };`

由于我将此对象传递给另一个服务来处理 _id 未知。

如何说服 PHP 驱动程序正确解析对象?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光倒影 2024-12-28 23:34:42

基本上我所做的就是这样的。

return json_encode(iterator_to_array($cursor));

但这创建了上述对象,这不是我需要的。

我就是这样解决的。

 $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++;
   }

返回 json_encode($return);

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);

初相遇 2024-12-28 23:34:42

如果为了节省 RAM 你的结果很大,你可以尝试这种更有效的方法:

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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文