0

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 Answer 1

0

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 ) . "\n";

?>

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

2
  • @codelover Not seeing how this is in the context of the question, but shouldn't your Content-Type be "application/json" as it's kind of been the standard for a while now. It also seems like your pushing this though something else maybe, which is resulting in your serialization woes. And perhaps that's another question related to what you are using. Commented Feb 10, 2014 at 12:02
  • Thanks. There was an issue in some other module, where it is forcing to convert as OBJECT, there it was using JSON_FORCE_OBJECT hence I got the issue. Which is completely irrelevant to the MongoDB :(. Probably I should delete this question. Commented Feb 10, 2014 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.