MongoDB将ObjectId转换为结果中的字符串

如果文档的字段值为 MongoId </ code>对象,则会返回给php 例如 http://dl.dropbox.com/u/7017985/Screenshots/26 .jpg ,有什么方法可以像简单的字符串那样返回它,而不是 MongoId </ code>对象。</ p>

为什么我需要它? 因为我需要将结果发送到javascript浏览器端。 我的文档有2-3个字段,它们是另一个文档的引用,它们保存为 ObjectId </ code>。 </ p>
</ div>

展开原文

原文

If document has fields with value as MongoId object, it will be returned to php something like http://dl.dropbox.com/u/7017985/Screenshots/26.jpg, is there any way how to return it like simple strings and not as MongoId object.

Why I need it ? Because I need to send result to javascript browser side. I have document which has 2-3 fields which is refs to another document, and they keep as ObjectId.

4个回答



MongoID支持 __ toString </ code>。 如果将其转换为字符串,或直接调用 __ toString </ code>,它会将值转换为字符串。</ p>
</ div>

展开原文

原文

MongoID supports __toString. If you cast it as a string, or call __toString directly, it will convert the value to a string.

donglulong0877
donglulong0877 我知道,但这意味着我必须完成整个结果:(这就是我想要避免的
7 年多之前 回复



我不相信有。 </ p>

MongoDB以BSON文档的形式获取输入和输出,其 ObjectId </ code>字段采用这种特殊形式。</ p>

这是 你不能改变的东西。</ p>
</ div>

展开原文

原文

I do not believe there is.

MongoDB takes input and output in the form of BSON documents whose ObjectId field takes this particular formation.

This is something you cannot change.

You could loop through the query results and convert all MongoId objects to strings. The below function will convert all ids if given either a single result array from MongoCollection::findOne(), or a MongoCursor result from MongoCollection::find().

function convert_mongoid_to_string(& $mongo_object)
{
    foreach($mongo_object as $mongo_key=>$mongo_element)
    {
        if(is_array($mongo_element)||is_object($mongo_element))
        {
            if(get_class($mongo_element) === "MongoId")
            {
                 //cast the object to the original object passed by reference
                 $mongo_object[$mongo_key]=(string)$mongo_element;
            }
            else
            {
                //recursively dig deeper into object looking for MongoId's
                convert_mongoid_to_string($mongo_element);
            }
        }
        else
        {
            //is scalar so just continue
            continue;
        }
    }
    return $mongo_object;
}
doudou521125
doudou521125 感谢您的解决方案,这对我来说是完全清楚的,但我已经搜索了数据库端解决方案,似乎不可能。
7 年多之前 回复

I've needed the MongoIds to be javascript/json (client side) friendly so I wanted to convert multiple mongoIds transparently. I couldn't find any build-in functionality to achieve this, but came up with the following solution:

<?php
function strToMongoIdObj(Array $_ids) {
    return array_map(function($id) {
            return new MongoId($id);
        }, $_ids);
}

function mongoIdToStr(MongoCursor $cursor) {
    $rs = array();
    foreach ($cursor as $doc) {
        $doc['_id'] = (string)$doc['_id'];
        $rs[] = $doc;
    }
    return $rs;
}

$_ids = array("522dbdd29076fde9057bb5ed",
              "522dbf229076fdeb053f5b7b");

$m = new MongoClient();
$cursor = $m->db->col
    ->find(array('_id' => array('$in' => strToMongoIdObj($_ids))));

print_r(mongoIdToStr($cursor));
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐