Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/webiny/mongo
[READ-ONLY] MongoDB PHP driver/layer. (master at Webiny/Framework)
https://github.com/webiny/mongo
Last synced: 5 days ago
JSON representation
[READ-ONLY] MongoDB PHP driver/layer. (master at Webiny/Framework)
- Host: GitHub
- URL: https://github.com/webiny/mongo
- Owner: webiny
- License: mit
- Created: 2014-08-30T18:26:41.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2017-11-26T21:25:01.000Z (about 7 years ago)
- Last Synced: 2024-12-07T21:43:59.378Z (26 days ago)
- Language: PHP
- Homepage: http://www.webiny.com/
- Size: 47.9 KB
- Stars: 1
- Watchers: 11
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
Mongo Component
=================Mongo Component is used for working with MongoDB database.
Install the component
---------------------
The best way to install the component is using Composer.```bash
composer require webiny/mongo
```
For additional versions of the package, visit the [Packagist page](https://packagist.org/packages/webiny/mongo).## Configuring Mongo service
The recommended way of using Mongo is by defining a Mongo service. Here is an example of defining a service:
```yaml
Mongo:
Services:
Webiny:
Class: \Webiny\Component\Mongo\Mongo
Arguments:
Uri: 127.0.0.1:27017
UriOptions: []
DriverOptions: []
CollectionPrefix: 'MyDatabase_'
Calls:
- [selectDatabase, [MyDatabase]]
Driver: \Webiny\Component\Mongo\Bridge\MongoDb
```Collection prefix will be automatically prepended for you to all database queries.
For more information see: [mongodb/mongo-php-library](http://mongodb.github.io/mongo-php-library/)
After you have defined your Mongo services (in most cases you will only need one, but you can have as many as you like), you can access your Mongo services by using `MongoTrait`:
```php
class MyClass {
use MongoTrait;public function test(){
// MongoTrait uses `Webiny` as a default service name
$this->mongo()->getCollectionNames();// If you have specified your own service name, pass it to mongo method
$this->mongo('MyMongo')->getCollectionNames();
}
}
```### ResultClass
`ResultClass` is used to wrap all Mongo command results. This allows us to have a compatibility layer in case something changes in Mongo response structures in the future
and also allows any developer to extend this class and add custom methods to handle mongo response flags.### Indexes
Currently Mongo component supports 3 types of indexes:
- SingleIndex
- CompoundIndex
- TextIndexTo create an index on your collection:
```php
// Create a unique single index object
$index = new SingleIndex('Name', 'name', false, true);// Use mongo trait to create index on your collection
$this->mongo()->createIndex('MyCollection', $index);
```Resources
---------To run unit tests, you need to use the following command:
$ cd path/to/Webiny/Component/Mongo/
$ composer.phar install
$ phpunitMake sure you set your MongoDb driver settings in `Tests\ExampleConfig.yaml`.