PHPackages                             lomocoin/php-mongo-transaction - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. lomocoin/php-mongo-transaction

ActiveProject[Database &amp; ORM](/categories/database)

lomocoin/php-mongo-transaction
==============================

Provide simple transaction feature for Mongodb

0.1.1(7y ago)31352MITPHPPHP &gt;=7.0

Since Mar 3Pushed 7y ago1 watchersCompare

[ Source](https://github.com/lomocoin/PHP-Mongo-Transaction)[ Packagist](https://packagist.org/packages/lomocoin/php-mongo-transaction)[ RSS](/packages/lomocoin-php-mongo-transaction/feed)WikiDiscussions master Synced yesterday

READMEChangelog (1)Dependencies (2)Versions (4)Used By (0)

PHP-Mongo-Transaction
=====================

[](#php-mongo-transaction)

Overview
--------

[](#overview)

Do you miss transaction in RDBMS while using MongoDB? You are not alone.

A news said that MongoDB will support ACID in the future, but what if we need it now?

Well, `PHP-Mongo-Transaction` provides a simply basic transaction feature similar to RDBMS.

The flow is simple, just begin a transaction, do something, then commit or rollback.

The concept to achieve this is also simple: build a record collection in MongoDB to trace the modification of the data, and recover when rollback.

That is, once a transaction wants to rollback, it will:

- delete what has been inserted
- insert back what has been deleted (with the same ID)
- replace the modified data with the original copy (with the same ID)

To achieve this, this lib wraps the basic `insertOne`, `updateOne`, `deleteOne` functions which provided by MongoDB Driver.

Have a look at `#Usage` part of this document to see how easy to use it.

Limitation
----------

[](#limitation)

1. We assume the database just works, so if there is a database failure, the transaction may not be rollback correctly and will be leaving as `ongoing` state. You may need to have a cron job to detect if everything works fine, and investigate manually when something goes wrong. If everything goes smoothly, the state of transactions should be either `commit` or `rollback`, except the real `init` and `ongoing` ones.
2. We can't handle concurrency issues at the current stage. It's too complicated to ensure data consist under the concurrent scenario. We recommend that you consider using a simple lock mechanism to avoid two transactions that may write to the same record happen at the same time, or adapting a message queue system to maintain the order of transaction execution.

Roadmap
-------

[](#roadmap)

- More unit tests
- Enhance docs
- Support `insertMany`, `updateMany`, `deleteMany`

Notice
------

[](#notice)

You can use `Persistable` class with this library, but do not do any magic in the `bsonSerialize` and `bsonUnserialize`, e.g: auto update the "last modified date".

**Any magic in these two functions will definitely destroy the valid data state**

Install
-------

[](#install)

```
composer require lomocoin/php-mongo-transaction
```

Usage
-----

[](#usage)

### 1. Require `autoload.php` file

[](#1-require-autoloadphp-file)

```
require __DIR__ . '/vendor/autoload.php';
```

### 2. Create transaction object

[](#2-create-transaction-object)

```
use Lomocoin\Mongodb\Config\TransactionConfig;
use Lomocoin\Mongodb\Transaction\Transaction;
use MongoDB\Client;

$dbName = 'lomocoin_mongodb_test';
$config = new TransactionConfig(
    new Client(),
    $dbName,
    'lomocoin_mongodb_test_transaction_log',
    'lomocoin_mongodb_test_transaction_state_change_log');

$transaction = Transaction::begin($config);
```

### 3. Make the change

[](#3-make-the-change)

#### 3.1 insert

[](#31-insert)

```
$collection = $config->getMongoDBClient()->$dbName->testCollection;

$transaction->insertOne($collection, [
    'username' => 'B',
    'email'    => 'b@example.com',
    'name'     => 'BB',
]);
```

#### 3.2 update

[](#32-update)

```
$transaction->updateOne($collection, [
    'username' => 'B',
], [
    '$set' => [
        'name' => 'BBB',
    ],
]);
```

#### 3.3 delete

[](#33-delete)

```
$transaction->deleteOne($collection, ['username' => 'B']);
```

### 4. Commit or Rollback

[](#4-commit-or-rollback)

```
// if no error happens, you commit
$transaction->commit();

// if any error happens, you rollback
$transaction->rollback();
```

Contributors
------------

[](#contributors)

[Shenghan Chen](https://github.com/zzdjk6) | [viest](https://github.com/viest)

License
-------

[](#license)

Apache License 2.0

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community11

Small or concentrated contributor base

Maturity50

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 86.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~136 days

Total

2

Last Release

2857d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/3908516?v=4)[Thor(Shenghan) Chen](/maintainers/zzdjk6)[@zzdjk6](https://github.com/zzdjk6)

---

Top Contributors

[![zzdjk6](https://avatars.githubusercontent.com/u/3908516?v=4)](https://github.com/zzdjk6 "zzdjk6 (33 commits)")[![viest](https://avatars.githubusercontent.com/u/14308107?v=4)](https://github.com/viest "viest (5 commits)")

---

Tags

phpmongodbtransaction

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/lomocoin-php-mongo-transaction/health.svg)

```
[![Health](https://phpackages.com/badges/lomocoin-php-mongo-transaction/health.svg)](https://phpackages.com/packages/lomocoin-php-mongo-transaction)
```

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k7.2M71](/packages/mongodb-laravel-mongodb)[doctrine/mongodb-odm

PHP Doctrine MongoDB Object Document Mapper (ODM) provides transparent persistence for PHP objects to MongoDB.

1.1k23.3M302](/packages/doctrine-mongodb-odm)[denchikby/phalcon-mongodb-odm

Phalcon MongoDB ODM

4212.8k](/packages/denchikby-phalcon-mongodb-odm)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
