PHPackages                             sokil/php-mongo - 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. sokil/php-mongo

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

sokil/php-mongo
===============

PHP Object Document Mapper for MongoDB

1.23.1(6y ago)239161.5k↓30.5%46[41 issues](https://github.com/sokil/php-mongo/issues)[1 PRs](https://github.com/sokil/php-mongo/pulls)9MITPHPPHP &gt;=5.4 || ^7.0CI failing

Since Jul 9Pushed 3y ago13 watchersCompare

[ Source](https://github.com/sokil/php-mongo)[ Packagist](https://packagist.org/packages/sokil/php-mongo)[ Docs](http://phpmongokit.github.io/)[ RSS](/packages/sokil-php-mongo/feed)WikiDiscussions 1.0 Synced 1mo ago

READMEChangelog (10)Dependencies (9)Versions (66)Used By (9)

Stand With Ukraine
==================

[](#stand-with-ukraine)

[![SWUbanner](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner-direct.svg)](https://github.com/vshymanskyy/StandWithUkraine/blob/main/docs/README.md)

---

PHPMongo ODM
============

[](#phpmongo-odm)

[![Total Downloads](https://camo.githubusercontent.com/329831359483a7eebf88601739c3128111cf4441e5e1646230e04d18419f4e27/687474703a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f736f6b696c2f7068702d6d6f6e676f2e7376673f31)](https://packagist.org/packages/sokil/php-mongo)[![Build Status](https://camo.githubusercontent.com/f249ea9035e587e50bd08beac0709fd15193f868233b59ace01cc9b49de5bba3/68747470733a2f2f7472617669732d63692e6f72672f736f6b696c2f7068702d6d6f6e676f2e706e673f6272616e63683d6d61737465722632)](https://travis-ci.org/sokil/php-mongo)[![Coverage Status](https://camo.githubusercontent.com/d1e80b90705f34e59552447a062dd6725cf50fea7441061efb1905e615925d0a/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f736f6b696c2f7068702d6d6f6e676f2f62616467652e706e67)](https://coveralls.io/r/sokil/php-mongo)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/b8e7ef24e1868a6a581e3d0f3e6ccdc0f0852584597571393008710a697f3b3e/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f736f6b696c2f7068702d6d6f6e676f2f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/sokil/php-mongo/?branch=master)[![Code Climate](https://camo.githubusercontent.com/1f368d43f08e0a402b370aa2d39395b5d97022822376dbcaa325a8e1c5ef4b88/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f736f6b696c2f7068702d6d6f6e676f2f6261646765732f6770612e737667)](https://codeclimate.com/github/sokil/php-mongo)[![Gitter](https://camo.githubusercontent.com/dec8b6b49273f8effc1aa09187106b398af3d7ee24dd47ba2aa02bdb93338f9e/68747470733a2f2f6261646765732e6769747465722e696d2f4a6f696e5f436861742e737667)](https://gitter.im/sokil/php-mongo?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)

#### PHP ODM for MongoDB.

[](#php-odm-for-mongodb)

⭐ Why to use this ODM? You can easily work with document data through comfortable getters and setters instead of array and don't check if key exist in array. Access to sub document uses dot-syntax. You can validate data passed to document before save. We give you events, which you can handle in different moments of document's life. You can create relations, build aggregations, create versioned documents, write [migrations](https://github.com/sokil/php-mongo-migrator) and do more things which makes your life easier.

🔢 Tested over MongoDB v.2.4.12, v.2.6.9, v.3.0.2, v.3.2.10, v.3.3.15, v.3.4.0. See [Unit tests](#unit-tests) for details.

[![ArmySOS - Help for Ukrainian Army](https://camo.githubusercontent.com/60ba4d7bf76f00ec067a309ed706f091ec14626fe86a8a237210ef266d831e3e/687474703a2f2f61726d79736f732e636f6d2e75612f77702d636f6e74656e742f75706c6f6164732f323031342f30392f3732385f39302e6a7067)](http://armysos.com.ua/en/help-the-army)

#### Requirements

[](#requirements)

- PHP-Mongo v.1
    - **PHP 5.3 not supported starting from 2018-10-19**
    - **PHP 5.4 - 5.5 not supported starting from 2019-10-19**
    - PHP 5.6
        - [PHP Mongo Extension](https://pecl.php.net/package/mongo) 0.9 or above (Some features require &gt;= 1.5)
    - PHP 7
        - [PHP MongoDB Extension](https://pecl.php.net/package/mongodb) 1.0 or above
        - [Compatibility layer](https://github.com/alcaeus/mongo-php-adapter). Please, note some [restriontions](#compatibility-with-php-7)
    - HHVM Driver [not supported](https://derickrethans.nl/mongodb-hhvm.html).
- PHP-Mongo v.2 (Currentry is under development and can not be used in production)
    - PHP 7
        - [PHP MongoDB Extension](https://pecl.php.net/package/mongodb) natively without legacy driver adapters.
    - PHP 5 not supported
    - HHVM Driver [not supported](https://derickrethans.nl/mongodb-hhvm.html).

#### Table of contents

[](#table-of-contents)

- [Installation](#installation)
    - [Common installation](#common-installation)
    - [Symfony bundle](#symfony-bundle)
    - [Laravel](#laravel)
    - [Yii component](#yii-component)
    - [Yii2 component](#yii2-component)
    - [Support of migrations](#support-of-migrations)
- [Connecting](#connecting)
- [Mapping](#mapping)
    - [Selecting database and collection](#selecting-database-and-collection)
    - [Custom collections](#custom-collections)
    - [Document schema](#document-schema)
- [Document validation](#document-validation)
- [Getting documents by id](#getting-documents-by-id)
- [Create new document](#create-new-document)
- [Get and set data in document](#get-and-set-data-in-document)
- [Embedded documents](#embedded-documents)
    - [Get embedded document](#get-embedded-document)
    - [Set embedded document](#set-embedded-document)
    - [Get embedded list of documents](#get-embedded-list-of-documents)
    - [Set embedded list of documents](#set-embedded-list-of-documents)
    - [Validation of embedded documents](#validation-of-embedded-documents)
- [DBRefs](#dbrefs)
- [Storing document](#storing-document)
    - [Storing mapped object](#storing-mapped-object)
    - [Insert and update documents without ODM](#insert-and-update-documents-without-odm)
    - [Batch insert](#batch-insert)
    - [Batch update](#batch-update)
    - [Moving data between collections](#moving-data-between-collections)
- [Querying documents](#querying-documents)
    - [Query Builder](#query-builder)
    - [Extending Query Builder](#extending-query-builder)
    - [Identity Map](#identity-map)
    - [Comparing queries](#comparing-queries)
- [Geospatial queries](#geospatial-queries)
- [Fulltext search](#fulltext-search)
- [Pagination](#pagination)
- [Persistence (Unit of Work)](#persistence-unit-of-work)
- [Deleting collections and documents](#deleting-collections-and-documents)
- [Aggregation framework](#aggregation-framework)
- [Events](#events)
- [Behaviors](#behaviors)
- [Relations](#relations)
    - [One-to-one relation](#one-to-one-relation)
    - [One-to-many relation](#one-to-many-relation)
    - [Many-to-many relation](#many-to-many-relation)
    - [Add relation](#add-relation)
    - [Remove relation](#remove-relation)
- [Concurency](#concurency)
    - [Optimistic locking](#optimistic-locking)
- [Read preferences](#read-preferences)
- [Write concern](#write-concern)
- [Capped collections](#capped-collections)
- [Executing commands](#executing-commands)
- [Queue](#queue)
- [Migrations](#migrations)
- [GridFS](#gridfs)
- [Versioning](#versioning)
- [Indexes](#indexes)
- [Caching and documents with TTL](#caching-and-documents-with-ttl)
- [Debugging](#debugging)
    - [Logging](#logging)
    - [Profiling](#profiling)
- [Unit tests](#unit-tests)

Installation
------------

[](#installation)

#### Common installation

[](#common-installation)

You can install library through Composer:

```
composer require sokil/php-mongo

```

Download latest release: [Latest sources from GitHub](https://github.com/sokil/php-mongo/releases/latest)

#### Compatibility with PHP 7

[](#compatibility-with-php-7)

> PHPMongo currently based on old [ext-mongo](https://pecl.php.net/package/mongo) entension. To use this ODM with PHP 7, you need to add [compatibility layer](https://github.com/alcaeus/mongo-php-adapter), which implement API of old extension over new [ext-mongodb](https://pecl.php.net/package/mongodb). To start using PHPMongo with PHP7, add requirement [alcaeus/mongo-php-adapter](https://github.com/alcaeus/mongo-php-adapter) to composer. Restrictions for using ODM with compatibility layer you can read in [known issues](https://github.com/alcaeus/mongo-php-adapter#known-issues) of original adapter.

Require adapter of old `ext-mongo` API to new `ext-mongodb`:

```
composer require alcaeus/mongo-php-adapter

```

#### Symfony bundle

[](#symfony-bundle)

If you use Symfony framework, you can use [Symfony MongoDB Bundle](https://github.com/sokil/php-mongo-bundle) which wraps this library

```
composer require sokil/php-mongo-bundle

```

#### Laravel

[](#laravel)

If you use Laravel framework, use [Laravel adapter](https://github.com/sokil/laravel-mongo-odm)

```
composer require phpmongokit/laravel-mongo-odm

```

#### Yii component

[](#yii-component)

If you use Yii Framework, you can use [Yii Adapter](https://github.com/sokil/php-mongo-yii) which wraps this library

```
composer require sokil/php-mongo-yii

```

This package in addition to PHPMongo adapter also has data provider and log router for MongoDb.

#### Yii2 component

[](#yii2-component)

If you use Yii2 Framework, you can use [Yii2 Adapter](https://github.com/PHPMongoKit/yii2-mongo-odm) which wraps this library

```
composer require phpmongokit/yii2-mongo-odm

```

#### Support of migrations

[](#support-of-migrations)

If you require migrations, you can add dependency to [Migrator](https://github.com/sokil/php-mongo-migrator), based on this library:

```
composer require sokil/php-mongo-migrator

```

Connecting
----------

[](#connecting)

#### Single connection

[](#single-connection)

Connecting to MongoDB server made through `\Sokil\Mongo\Client` class:

```
