dot dot - 4 months ago 28
PHP Question

how to rewrite mongo client code using mongodb driver instead

Background Information:

I have a php script that parses a text file and uses it to insert data into a mongo database. I'm moving away from the old mongo php driver to use the MongoDB\Driver classes instead (http://php.net/manual/en/book.mongodb.php)

Problem

There's one section of code that I'm trying to rewrite using the new classes but I can't seem to get it working - aka its not writing data to the database... and I don't know how to find the error either.

Old Code:

This is the old code:

//database variables
$m = new MongoClient("mongodb://127.0.0.1:27017");

$db = "locations";
function update_location_status($location)
{
global $m, $db;
$collection = $db->locations;
echo "<pre>".json_encode($status)."</pre>";

try {
$cursor = $collection->update(array('domain'=>$status['domain']) , array('$set'=>$status), array('upsert'=>true) );
echo "Successfully upserted location status record for ". $location['domain'] ." into database...<BR>";
return true;
} catch(MongoCursorException $e) {
var_dump($cursor);
return false;
}


New Code

$m = new \MongoDB\Driver\Manager("mongodb://127.0.0.1:27017");
function update_location_status($location)
{
global $m;
echo "<pre>".json_encode($status)."</pre>";
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->update(
[array('domain'=>$status['domain'])],
[array('domain'=>$status)],
['multi' => false, 'upsert' => true]
);

try{
$writeConcern = new MongoDB\Driver\WriteConcern(MongoDB\Driver\WriteConcern::MAJORITY, 1000);
$result = $m->executeBulkWrite('db.locations', $bulk, $writeConcern);
if($result->getInsertedCount()){
return true;
}else{
echo "<font color=red><pre>";
var_dump($result->getWriteErrors());
echo "</pre></font>";

return false;
}
} catch(MongoCursorException $e) {
var_dump($result);
return false;
}


Results

My attempt to dump the error message using the getWriteErrors returns an empty array:

array(0) {
}


Any suggestions would be appreciated.

EDIT 1

So I've updated the code and it's creating data, but it looks different from the previous / old code.
This is what the data from the old code looks like:

{ "_id" : ObjectId("584586312a0d63c2e21eee12"),
"bbn" : "133",
"customized" : true,
"nep" : "812",
"pdate" : ISODate("2017-02-05T06:44:41Z"),
"domain" : "test.mydomain", "status" : false, "t_name" : "agcab", "type" : "current_status"
}


And with this code:

global $m, $db;
echo "<pre>".json_encode($status)."</pre>";
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->update(
['domain'=>$status['domain']],
['$set'=>['domain'=>$status]],
['multi' => false, 'upsert' => true]
);


the data ends up looking like:

{ "_id" : ObjectId("5910da5d99077b6b4887582f"),
"domain" : {
"bbn" : "133",
"nep" : "812",
"type" : "current_status",
"pdate" : ISODate("1970-01-18T07:00:12.882Z"),
"status" : false,
"domain" : "test.mydomain",
"tenant_name" : "agcab",
"customized" : true
}
}


I don't want it nested under another "domain" array.

Answer Source

You should update your code to below.

 $bulk->update(
     ['domain'=>$status['domain']],
     ['$set'=>$status],
     ['multi' => false, 'upsert' => true]
 );

$result = $m->executeBulkWrite(dbname.collectionname, $bulk, $writeConcern);