16

I have an Android application which sends json data to PHP script. The PHP script have to save the data into MongoDB.

When I insert the data, MongoDB treat this data as a string.

$conn = new Mongo('mongodb://127.0.0.1:27017');

// access database
$db = $conn->Data;

// access collection
$collection = $db->Collection;
$collection->insert($myjson)

How can I say to MongoDB PHP driver that it is already a json document?

Thanks

5 Answers 5

20

The PHP MongDB Driver accepts arrays for inserts and queries (see here: http://www.php.net/manual/en/mongo.queries.php)

So you need to convert your JSON to an array.

Luckily, in general this is quite easy ... here is a snippet from a longer piece of code (see this article) to insert JSON data from the Twitter API into an array, then into MongoDB:

// Convert JSON to a PHP array
$usertimeline = json_decode($usertimeline);

// Loop array and create seperate documents for each tweet
foreach ($usertimeline as $id => $item) {
   $collection->insert($item);
}

Note the json_decode() function can convert to an array by passing true as the second parameter.

Sign up to request clarification or add additional context in comments.

1 Comment

How would you handle inserting native Mongo data types like MongoDate?
6

Here's a tip for others who want to do this, but want to update records rather than insert new ones. In order to get Justin's approach to work, I had to make sure that I convert the _id object for each item to a MongoId:

// Convert JSON to a PHP array
$usertimeline = json_decode($usertimeline);

// Loop array and create seperate documents for each tweet
foreach ($usertimeline as $id => $item) {
   $mongo_id = new MongoId($id);
   $item->_id = $mongo_id;
   $collection->update(array('_id' => $mongo_id), $item);
}

1 Comment

Small update if someone finds this issue in 2024, use MongoDB\BSON\ObjectID
1

As MongoDB PHP library supports conversion from JSON to BSON and BSON to PHP Std Class Object, You can follow simple steps.

    //Convert Row (Document) to json
    $json_row = json_encode($row);

    //Convert JSON to BSON
    $bson = \MongoDB\BSON\fromJSON($json_row);

    //Convert BSON to PHP Std Class object
    $row = \MongoDB\BSON\toPHP($bson);


    // Insert Record to Document
    $collection->insertOne($row);

You can get full MongoDB database backup and restore script from here https://github.com/sulochanatutorials/php-mongodb-backup-restore

1 Comment

Please edit your answer to include an explanation of how this works and why it is of solution to the problem described in the question. See How to Answer.
0

MongoDB uses BSON not JSON so you need to re-encode anyways. Also, I remember talking to kchodorow about this and the raw interface was not exposed then (been half a year at least) and I doubt it is now so even if you manage to get BSON data from somewhere , you can't avoid the de/encode.

Comments

0

UPDATE 2021 WITH PHP 7 DRIVER:

Lets say your json string is

$myjson = {"name":"foo", "age":30}

First decode it with PHP json_decode. Which returns an array.

$myarraydata = json_decode($myjson)

Now you can directly insert it into mongoDB

$bulk = new MongoDB\Driver\BulkWrite(['ordered' => true]);
$bulk->insert($myarraydata);
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $connection->executeBulkWrite('dbname.collection_name', $bulk, $writeConcern);

Check bulk operation official documentation here.

Comments

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.