Transaction in MongoDB 4.2 with new PHP Driver - php

I am new to MongoDB as I was a SuperFan of MySQL before. I recently moved to this NoSQL thing and loved it but now I am badly trapped at Transactions in MongoDB.
I found some related questions on SO but with no answers or obsolete which does not work with new MongoDB PHP Driver as there are many changes in syntax/functions and I could see many newbie like me are confused between MongoDB Docs and PHP Driver.
I found this way of committing transactions in MongoDB Docs
$client = new MongoDB\Driver\Manager("mongodb://127.0.0.1:27017");
$callback = function (\MongoDB\Driver\Session $session) use ($client)
{
$client->selectCollection('mydb1', 'foo')->insertOne(['abc' => 1], ['session' => $session]);
$client->selectCollection('mydb2', 'bar')->insertOne(['xyz' => 999], ['session' => $session]);
};
// Step 2: Start a client session.
$session = $client->startSession();
// Step 3: Use with_transaction to start a transaction, execute the callback, and commit
$transactionOptions =
[
'readConcern' => new \MongoDB\Driver\ReadConcern(\MongoDB\Driver\ReadConcern::LOCAL),
'writeConcern' => new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, 1000),
'readPreference' => new \MongoDB\Driver\ReadPreference(\MongoDB\Driver\ReadPreference::RP_PRIMARY),
];
\MongoDB\with_transaction($session, $callback, $transactionOptions);
but this syntax/functions are obsolete for new PHP Driver and it gives following error
Call to undefined function MongoDB\with_transaction()
According to PHP Docs, the new PHP Driver for MongoDB provides these options to commit transaction but I don't understand how? because there is no example given in docs.
https://www.php.net/manual/en/mongodb-driver-manager.startsession.php
https://www.php.net/manual/en/mongodb-driver-session.starttransaction.php
https://www.php.net/manual/en/mongodb-driver-session.committransaction.php
My Question is, How can I update the above code with New PHP Driver's functions? I believe to use
MongoDB\Driver\Manager::startSession
MongoDB\Driver\Session::startTransaction
MongoDB\Driver\Session::commitTransaction
but I don't understand what their syntax is or their arguments etc because of incomplete documentation and no examples. Thanking you in anticipation for your time and support.

Ok, So, I found the answer to my question and I thought it can be helpful for some others
using Core Mongo Extension
$connection = new MongoDB\Driver\Manager("mongodb://127.0.0.1:27017");
$session = $connection->startSession();
$session->startTransaction();
$bulk = new MongoDB\Driver\BulkWrite(['ordered' => true]);
$bulk->insert(['x' => 1]);
$bulk->insert(['x' => 2]);
$bulk->insert(['x' => 3]);
$result = $connection->executeBulkWrite('db.users', $bulk, ['session' => $session]);
$session->commitTransaction();
using PHP Library
$session = $client->startSession();
$session->startTransaction();
try {
// Perform actions.
//insertOne(['abc' => 1], ['session' => $session]); <- Note Session
$session->commitTransaction();
} catch(Exception $e) {
$session->abortTransaction();
}
Note: To make the answer short and to the point, I have omitted some of the optional parameters and used a dummy insert data etc without any try-catch.
If you are running MongoDB instance as standalone version that is for development or testing purpose then you might get error something like
transaction numbers are only allowed on a replica set member or mongos
Then you can enable Replica on a standalone instance following this guide https://docs.mongodb.com/manual/tutorial/convert-standalone-to-replica-set/

Related

How can I integrate elasticsearch to mysql using php

I was working on a project which is required to use elasticsearch. I followed the guide: https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/index.html
It works perfectly for me:
require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$hosts = [
'myhost'
];
$client = ClientBuilder::create() // Instantiate a new ClientBuilder
->setHosts($hosts) // Set the hosts
->build();
$params = [
'index' => 'php-demo-index',
'type' => 'doc',
'id' => 'my_id',
'body' => ['testField' => 'abc']
];
$response = $client->index($params);
print_r($response);
Now, that's only a basic thing. Now, what I want is to integrate this with Mysql i.e. as I update or insert into my table in database, it get indexed automatically in elasticsearch.
I know, we have Logstash that can query db constantly after a given interval and index into elasticsearch. But, I want indexing to be happened automatically after insertion into db using PHP without logstash.
I know such a library in (nodeJs+mongodb) ie. mongoosastics: https://www.npmjs.com/package/mongoosastic. Is there any library available in php which can do such a task automatically. Please provide me the sample code, if you know one.
There is indeed libraries to automate this task. However it generally requires the use of an ORM like Doctrine in order gracefully hook in to your database implementation. If you are able to use the Symfony framework in your project there is a library called FOSElasticaBundle which keeps your indices in sync with your database operations.

Call to undefined method MongoDB::update()

when using the update statement via PHP means am getting the error.
Call to undefined method MongoDB::update()
I'm using the mongo-php-driver driver for connecting the MongoDB and PHP
My Code
$result = $this->mongo_db->update('table_name',array('user_type'=>"D"),array('$set'=>array('account_balance'=>0)),array('upsert'=>true));
My MongoDB reference link is https://docs.mongodb.com/manual/reference/method/db.collection.update/
Hi am using updateMany instead of update. Its working fine for me.
$result = $this->mongo_db->updateMany('table_name',array('user_type'=>"D"),array('$set'=>array('account_balance'=>0)),array('upsert'=>true));
From the documentation
Use the MongoDB\Collection::updateOne() method to update a single document matching a filter. MongoDB\Collection::updateOne() returns a MongoDB\UpdateResult object, which you can use to access statistics about the update operation.
<?php
$collection = (new MongoDB\Client)->test->users;
$collection->drop();
$collection->insertOne(['name' => 'Bob', 'state' => 'ny']);
$collection->insertOne(['name' => 'Alice', 'state' => 'ny']);
$updateResult = $collection->updateOne(
['state' => 'ny'],
['$set' => ['country' => 'us']]
);
printf("Matched %d document(s)\n", $updateResult->getMatchedCount());
printf("Modified %d document(s)\n", $updateResult->getModifiedCount());
For Extra Read:
The MongoDB PHP Library and underlying mongodb extension have notable API differences from the legacy mongo extension. Please read the upgrade guide for more details.
You should check if the mongo db extension is installed and available. Install it with pecl:
https://stackoverflow.com/a/74844843/2862728

php mongodb library abortTransaction doest't work

i'm trying to learn mongodb transactions using php-mongodb library v1.5 but i've found some problemes.
i've tried to start, commit, and abort transaction using the giving methods but abortTransaction is not working for me :
$session = self::$instance->startSession();
$this->db = self::$instance->{"mydb"};
$session->startTransaction();
$this->db->users->deleteOne([
'_id' => new MongoDB\BSON\ObjectId('5c88e197df815495df201a38')
]);
$session->abortTransaction();
$session->endSession();
the transaction is always commited even after the abort action !!!
what i'm missing here please save my day :(
the transaction is always commited even after the abort action
This is because the delete operation doesn't utilise the session object that you have instantiated. You need to pass the session as a $options parameter MongoDB\Collection::deleteOne(). Otherwise it will execute outside of the transaction. For example:
$session->startTransaction();
$this->db->users->deleteOne(
['_id' => new MongoDB\BSON\ObjectId('5c88e197df815495df201a38')],
['session' => $session]
);
See also MongoDB Transactions for more information

MapReduce with PHP and MongoDB

I'm fairly new to Mongo and I have what I thought was a simple question. How do I do MapReduce with PHP and the non legacy MongoDB driver http://php.net/manual/en/set.mongodb.php or the higher level package mongodb/mongodb found at https://packagist.org/packages/mongodb/mongodb?
Every example I've seen seems to use the legacy driver (http://php.net/manual/en/book.mongo.php). They all use the MongoCode object, which doesn't exist in mongodb.php. It exists in mongo.php (the legacy driver). When I try and use it, it will say that "Class 'MongoCode' not found".
My code looks something like:
$function = "function() { emit(this); }";
$map = new \MongoCode($function);
$command = $db->command([
"mapreduce" => "db.archiveData",
"map" => $map,
"query" => $query,
"out" => "data"
]);
To make things more confusing, when I look at the source at https://github.com/mongodb/mongo-php-library, there is a unit test for MapReduce (https://github.com/mongodb/mongo-php-library/blob/4dc36f6231df133a57ff0dc5a0123945133d25ba/tests/Operation/MapReduceFunctionalTest.php). But it uses the MongoDB\Operation\MapReduce, which doesn't seem to exist in the 1.1 version of mongodb/mongodb.
I thought maybe I would call it on the server using JavaScript. But when I look at http://php.net/manual/en/mongodb.execute.php, it says it "is deprecated in MongoDB 3.0+". So that doesn't feel like something I should use.
So is it that:
MapReduce is not supported with mongodb/mongodb. Or maybe it is not supported yet, but will be?
I have to use the legacy driver for MapReduce?
I have to figure out a way to call db.collection.mapReduce via JavaScript on the server?
I have to use the Aggregation Pipeline (https://docs.mongodb.com/manual/aggregation/) to do map reduce type of actions? But that feels much more limited.
What am I missing?
So I now have clarity on where things are at.
MapReduce will be officially supported in 1.2.0 of PHPLib (https://jira.mongodb.org/browse/PHPLIB-53)
Until then, there is a completely usable workaround by using the command object as per https://docs.mongodb.com/php-library/current/upgrade/#mapreduce-command-helper
Example is here as well:
$database = (new MongoDB\Client)->selectDatabase('db_name');
$cursor = $database->command([
'mapReduce' => 'collection_name',
'map' => new MongoDB\BSON\Javascript('...'),
'reduce' => new MongoDB\BSON\Javascript('...'),
'out' => 'output_collection_name',
]);
$resultDocument = $cursor->toArray()[0];
You can also use MapReduce via Doctrine (http://docs.doctrine-project.org/projects/doctrine-mongodb-odm/en/latest/reference/map-reduce.html), but that is using legacy and a shim. So probably not a good choice for a new project.

Insert MongoDB ISODate PHP

I am using the following versions:
PHP -> 5.6.11
MongoDB -> 3.2
MongoDB PHP Driver -> 1.1
When I was looking to install a MongoDB PHP driver here I noticed that the driver named "Mongo" had been deprecated and proceeded to follow the provided link to install the new extension "MongoDB". This will be referred to as php_mongodb. Since I am using a Windows System I had to copy the file php_mongodb.dll to my ../php/ext and added the line extension=php_mongodb.dll to my PHP.ini
Using the now deprecated driver I used to be able to insert a MongoDate() as shown below.
<?php
$connection = new MongoClient();
$database = $connection->selectDB('test');
$coll = new MongoCollection($database, 'users');
$coll->insert(
(object)array(
"createdAt" => new MongoDate()
)
);
?>
The Problem is that with the new php_mongodb this MongoDate() does not seem to be available I receive the error: Fatal error: Class 'MongoDate' not found
What is the equivalent to this using new php_mongodb driver?
Should I consider downgrading my version of MongoDB to 3.0 so that I can use the now legacy Mongo Driver?
Is there a PHP native way to make a date that is of type ISODate?
Should I consider downgrading my version of MongoDB to 3.0 so that I can use the now legacy Mongo Driver?
This is what I've tried to no avail, the following will add a value of type Timestamp to a Document, however it seems that because it is not of type ISODate that I am unable to enforce TTL with this createdAt field:
<?php
require '/vendor/autoload.php';
$date = new MongoDB\BSON\Timestamp(1, date('U'));
$manager = new MongoDB\Driver\Manager("mongodb://127.0.0.1:27017");
$collection = new MongoDB\Collection($manager, "test.users");
$ex = [
"createdAt" => $date
];
$result = $collection->insertOne( $ex );
?>
Another thing I have tried is the below date, however without the MongoDate() functionality I do not know how to insert this as the type ISODate for MongoDB:
date(DATE_ISO8601, date('U'));
not sure if you still need this, but i have been struggling a lot with this. Finally i have been able to figure it out.
$orig_date = new DateTime('2016-01-22 14:00:00');
# or you can use any other way to construct with (int timestamp)
$mongo_date = new MongoDB\BSON\UTCDateTime($orig_date->getTimestamp());
$filter = [
....
....
['timestamp' => ['$gte' => $mongo_date]],
....
....
]
# create command (for example aggregation)
$cmd = new MongoDB\Driver\Command( $filter );
# execute command
$cursor = $manager->executeCommand('my_mongo_db_name', $cmd);
This code works for me.

Categories