PHP is not a good language talking to MongoDB


  • administrators

    It's just too hard. Even for finding a doc. Found this live saver:

    The BSONDocument object has a jsonSerialize method. Use that:
    
    Example
    
    {"_id" : 12345,
        "filename" : "myfile",
        "header" : {
            "version" : 2,
            "registry" : "test",
            "serial" : 20080215,
            "records" : 17806,
            "startDate" : 19850701,
            "endDate" : 20080214
        },
    }
    
    $connect = new MongoDB\Client('mongodb://yourconnection');
    $db = $connect->YourDB;
    $collection = $db->YourCollection;
    
    $test = $collection->findOne(array("_id"=>12345));
    $data = $test->jsonSerialize();
    
    echo $data->_id;
    echo $data->filename;
    Will output this:
    
    12345
    myfile