dongyoudi1342 2014-02-10 10:34
浏览 63
已采纳

PHP MongoDB - 将数组保留为数组而不是对象

This is the object which I inserted to mongodb.

{
    '_array': [1,2,3],
    '_ass_array': [{'num': 1, 'name': 'One'}], 
    '_obj': {'key': 'val'}
}

If I query on terminal, I can get the same as I inserted. But while querying via PHP's MongoCollection::findOne()/ MongoCollection::find(), I am getting following format.

{
    '_array': { 0: 1, 1: 2, 2: 3},
    '_ass_array': {
       0: {'num': 1, 'name': 'One'}
     }, 
    '_obj': {'key': 'val'}
}

Is there any way I can get the array as array and object as object? if required, I can update the question with the complete code.

  • 写回答

1条回答 默认 最新

  • duanjianfu1398 2014-02-10 11:10
    关注

    On the same data this works for me. Just wondering if you are doing something different in a serialization since the JSON syntax in the post. As expected $data is a PHP array.

    <?php
    
    $dbhost = 'localhost';
    $dbname = 'test';
    
    $m = new Mongo("mongodb://$dbhost");
    $db = $m->$dbname;
    
    $col = $db->data;
    
    $data = $col->findOne();
    
    echo json_encode( $data ) . "
    ";
    
    ?>
    

    A little intrigued how you arrived at your result, as various people here have that format in their MongoDB documents.

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

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度