doulu4233 2013-09-06 11:31
浏览 43
已采纳

理解从PHP文件访问的JSON结果中的“_id”键。

PHP

$query = $myCollection->findOne(array("field2.sf2" => "value two"));
echo json_encode($query);

Returned JSON Object

{"_id":{"$id":"5476854783473474578548"},"field1":"value one","field2":{"sf1":["av1","av2","av3"],"sf2":"value two"},"field3":"value three"}

What is happening at:

"_id":{"$id":"5476854783473474578548"}

I can see it is the representation of the Document's "_id" key and value ie:

"_id": ObjectId("5476854783473474578548")

But a few things are happening:

  • The value becomes a sub document ie it is surrounded in curly braces
  • ObjectId is being replaced by "$id"

I'm using MongoDB, accessed by a PHP file, via jQuery's getJSON() method.

Are there any gotcha's i need to look out for with this happening?

Any commonly known 'industry knowledge' tips that could be helpful to a MongoDB newbie or further explain what is happening?

  • 写回答

1条回答 默认 最新

  • doulvli9462 2013-09-06 11:41
    关注

    The objectId Is not being replaced but merely serialised to string form when you call json_encode.

    Its properties are being taken out, placed inside stringified JSON and sent over the wire where then in JQuery the library parses that stringified JSON into the object you see.

    No data has been lost.

    The only gotcha I can think of is that the constructor for MongoId does not actually take this object back in, it assumes only a hexadecimal ObjectId (i.e. 5476854783473474578548) http://www.php.net/manual/en/mongoid.construct.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大