The transactions are visibly not supported.
https://github.com/jenssegers/laravel-mongodb#database-testing
Keep in mind that these traits are not yet supported:
- use Database Transactions;
- use RefreshDatabase;
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
In my ubuntu 20 I have MongoDB 6 installed so in mongosh I check test
test> db.version()
6.0.0
In laravel site with jenssegers/mongodb I try to apply transaction and looking at https://github.com/jenssegers/laravel-mongodb/pull/2014 branch in file config/database.php I added replicaSet option :
'mongodb' => [
'driver' => 'mongodb',
'host' => env('DB_HOST', '127.0.0.1'),
'port' => env('DB_PORT', 27017),
'database' => env('DB_DATABASE', 'homestead'),
'username' => env('DB_USERNAME', 'homestead'),
'password' => env('DB_PASSWORD', 'secret'),
'options' => [
'replicaSet' => 'rs',
'database' => env('DB_AUTHENTICATION_DATABASE', 'admin'),
],
'dsn' => env('MONGO_DB_URI','mongodb://').env('MONGO_DB_HOST', 'localhost')
],
Next I run succussfully command
npm install run-rs -g
But I had to run it under sudo.
Next I run :
hoster@hoster-os:/MyProject$ run-rs -v 4.0.0 --shell
Downloading MongoDB 4.0.0
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 67.3M 100 67.3M 0 0 9377k 0 0:00:07 0:00:07 --:--:-- 9339k
I suppose I do not need to install and download MongoDB 4.0.0 So I breaked the command above
hoster@hoster-os:/MyProject$ sudo run-rs -v 6.0.0 --shell
[sudo] password for hoster:
Downloading MongoDB 6.0.0
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 243 0 243 0 0 791 0 --:--:-- --:--:-- --:--:-- 791
gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error is not recoverable: exiting now
Error: Command failed: tar -zxvf mongodb-linux-x86_64-ubuntu1604-6.0.0.tgz
gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error is not recoverable: exiting now
at checkExecSyncError (node:child_process:841:11)
at execSync (node:child_process:912:15)
at download (/usr/lib/node_modules/run-rs/src/download.js:79:5)
at run (/usr/lib/node_modules/run-rs/index.js:82:20)
at run.next (<anonymous>)
at onFulfilled (/usr/lib/node_modules/run-rs/node_modules/co/index.js:65:19)
at /usr/lib/node_modules/run-rs/node_modules/co/index.js:54:5
at new Promise (<anonymous>)
at co (/usr/lib/node_modules/run-rs/node_modules/co/index.js:50:10)
at Object.<anonymous> (/usr/lib/node_modules/run-rs/index.js:32:1)
at Module._compile (node:internal/modules/cjs/loader:1126:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1180:10)
at Module.load (node:internal/modules/cjs/loader:1004:32)
at Function.Module._load (node:internal/modules/cjs/loader:839:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
I am not sure if I need to run last command ? What is it ? How can I check what I have installed ?
Running my app with
'replicaSet' => 'rs',
option I got run time error :
MongoDB \ Driver \ Exception \ ConnectionTimeoutException
Without option
'replicaSet' => 'rs',
I can run my app, so with restarted mongo service :
hoster@hoster-os:/MyProject$ sudo systemctl status mongod
● mongod.service - MongoDB Database Server
Loaded: loaded (/lib/systemd/system/mongod.service; disabled; vendor preset: enabled)
Active: active (running) since Sat 2022-09-03 07:39:07 EEST; 3s ago
Docs: https://docs.mongodb.org/manual
Main PID: 14620 (mongod)
Memory: 193.8M
CGroup: /system.slice/mongod.service
└─14620 /usr/bin/mongod --config /etc/mongod.conf
Sep 03 07:39:07 hoster-os systemd[1]: mongod.service: Succeeded.
Sep 03 07:39:07 hoster-os systemd[1]: Stopped MongoDB Database Server.
Sep 03 07:39:07 hoster-os systemd[1]: Started MongoDB Database Server.
having code with error in my control :
try {
$session = DB::getMongoClient()->startSession();
$session->startTransaction();
$userSubscription = UserSubscription
::getBySubscriptionId($subscriptionId)
->getByUserId($request->userId)
->first();
if (empty($userSubscription)) {
$userSubscription = UserSubscription::create([
'subscription_id' => $subscriptionId,
'user_id' => $request->userId,
]);
}
$actionsHistory = \App::make(App\Library\Services\ActionsHistoryServiceInterface::class);
$ret = $actionsHistory->addLog(
model: $useERRORrSubscription, // LINE WITH NONEXISTING VAR - ERROR WOULD BE RAISED
action_type: ActionHistoryType:: AHT_USER_SUBSCRIBED_TO_SUBSCRIPTION,
body:
' User ' . $request->userId . '=>' . $user->name . ' (' . $user->email . ') was subscribed to subscription ' . $subscriptionId . '=>' . $subscription->title
);
$session->commitTransaction();
} catch (Exception $e) {
$session->abortTransaction();
return response()->json([ 'message' => $e->getMessage() ], HTTP_RESPONSE_INTERNAL_SERVER_ERROR);
}
Checking /telescope/queries I see only 1 insertOne command and not any transaction commands, as I expected : https://prnt.sc/DtYwx-DS-lUc
What have I to do to run Mongo transaction in my site ?
laravel/framework "^9.26.1",
"jenssegers/mongodb": "^3.9.1"
Thanks in advance !
Please or to participate in this conversation.