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.

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

报告相同问题?

悬赏问题

  • ¥30 Android STD快速启动
  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动