I'm working on a migration project from php5.6 to 7.x in which I have a json with special characters in keys. How can I insert this to mongodb?
In php5.6 I was using MongoClient as the driver which internally converts the special characters and hence I was able to insert the document. But I'm not able to achieve the same in php7. I'm using MongoDB\Driver\Manager to connect to Mongodb.
EDIT:
I have a set of api's coming from another system with the below format
$content_array=
{
"context": {
"extensions": {
"http://id.tincanapi.com/extension/platform": "web",
"http://id.tincanapi.com/extension/browser-info": "Mozilla/5.0",
"http://id.tincanapi.com/extension/host-session-id": "4e320015"
}
}
}
$dbconnection = new MongoDB\Client($uri);
$db = $dbconnection->$dbname;
$collection = $db->$dbcoll;
$collection->insertOne(json_decode($content_array));
It fails in the above , saying "Invalid document for insert: keys cannot contain \".\":in the key". I understand its a not a good practice to use these characters in the key, however since this piece of code works in php5.6 using MongoClient I wanted to keep the changes as minimal as possible when migrating to php7.
EDIT 2:
The piece of code which works in php5.6
$dbconnection = new \MongoClient($dbhost, $options);
if (!empty($dbconnection)) {
$db = $dbconnection->selectDB($dbname);
$collection = $db->selectCollection($dbcoll);
$status = 0;
if (!empty($collection)) {
$collection->insert(json_decode($content_array));
$status = 1;
}
EDIT3:
part of me was wrong in not giving all details at once.. i do a json_decode to convert to native php array before inserting to mongo.
Any help would be appreciated. Thanks.