GithubHelp home page GithubHelp logo

isabella232 / mongo-2 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from webiny/mongo

0.0 0.0 0.0 49 KB

[READ-ONLY] MongoDB PHP driver/layer. (master at Webiny/Framework)

Home Page: http://www.webiny.com/

License: MIT License

PHP 100.00%

mongo-2's Introduction

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:

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

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
        $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
  • 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);

Resources

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.

mongo-2's People

Contributors

svenalhamad avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.