PHP MongoDb Class Wrapper
Abstraction layer for MongoDb.

Mongo Component

Mongo Component is used for working with MongoDB database.

Install the component

The best way to install the component is using Composer.

composer require webiny/mongo

For additional versions of the package, visit the Packagist page.

Configuring Mongo service

The recommended way of using Mongo is by defining a Mongo service. Here is an example of defining a service:

            Class: \Webiny\Component\Mongo\Mongo
                UriOptions: []
                DriverOptions: []
                CollectionPrefix: 'MyDatabase_'
                - [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

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:

class MyClass {
    use MongoTrait;

    public function test(){
        // MongoTrait uses `Webiny` as a default service name

        // If you have specified your own service name, pass it to mongo method


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.


Currently Mongo component supports 3 types of indexes:

  • SingleIndex
  • CompoundIndex
  • TextIndex

To create an index on your collection:

// 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);


To run unit tests, you need to use the following command:

$ cd path/to/Webiny/Component/Mongo/
$ composer.phar install
$ phpunit

Make sure you set your MongoDb driver settings in Tests\ExampleConfig.yaml.

To learn what you can do with Webiny Framework, checkout Webiny Platform - open-source content management system.

Explore Webiny Framework components:

  • Amazon currently supports implementation of Amazon S3
  • Annotations component for parsing annotations from a class, method or a property
  • Cache provides several caching libraries like Apc, Couchbase, Memcache and Redis
  • ClassLoader a PSR-0, PSR-4 and PEAR class loader
  • Config a very handy library for parsing YAML, INI, JSON and PHP configuration files
  • Crypt library for encoding, decoding and validating hashes
  • Entity an ODM layer (currently only supports MongoDB)
  • EventManager want to do event-based development, this is a library for you
  • Http library for parsing HTTP requests and creating HTTP responses
  • Image library for image manipulation
  • Logger PSR-3 log components for logging code execution in runtime
  • Mailer component for sending emails
  • Mongo MongoDB class wrapper
  • OAuth2 library for working with OAuth2, currently supports Facebook, LinkedIn and Google+
  • REST fully featured REST library with caching, security and rate control
  • Router handles defining, parsing, creating and matching url routes
  • Security provides authorization and authentication layer
  • ServiceManager want to write truly service based, loosely-coupled code, this library provides that
  • StdLib this component provides object wrappers for Arrays, Strings, Urls and DateTime data types
  • Storage storage abstraction layer that simplifies the way you work with files and directories
  • TemplateEngine provides a layer for rendering view templates and defining template plugins and manipulators
  • TwitterOAuth library for working with Twitter API using Twitter OAuth


Lastest from our blog:

The Power of the Community

In the focus of recent feedback that I got, I think there is a need for me to justify some of the decisions we made while developing Webiny Framework.

Share & subscribe: