photon / utils-mongodb
Collections of small tools for MongoDB.
Installs: 1 302
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: ^5.5 || ^7.0
- mongodb/mongodb: ~1.1
- photon/photon: ~2.0
README
Various useful tools for MongoDB
PHP Versions
- 5.6, 7.0 and 7.1 are supported and tested under travis
- Use ext-mongodb and mongodb/mongodb. Do not works anymore with legacy ext-mongo
Quick start
-
Add the module in your project
composer require "photon/utils-mongodb:dev-master"
or for a specific version
composer require "photon/utils-mongodb:2.0.0"
-
Define a MongoDB connection in your project configuration
'databases' => array( 'default' => array( 'engine' => '\photon\db\MongoDB', 'server' => 'mongodb://localhost:27017/', 'database' => 'utils', 'options' => array( 'connect' => true, ), ), ),
-
Enjoy !
Counters
The counter class implement a atomic counter increment and retreive. It's a thread safe auto-increment.
-
Create a class to define a counters collection
class MyCounter extends \photon\utils\mongodb\Counter { const database = 'default'; const collection = 'counters'; }
-
Read / Write counter
$value = MyCounter::get('foo'); // = 0 $value = MyCounter::inc('foo'); // = 1 $value = MyCounter::get('bar'); // = 0 $value = MyCounter::get('foo'); // = 1