98 online 3 members  + Join RP Base  +
FreeScript

php-mongo

by sokil  ·  version 2026.10.20  ·  updated 2 days ago

No reviews yet

Downloads
0
Reviews
0
Size
113 KB
Code check
Clean
Licence
MIT
Released
Nov 2026

MongoDB ODM. Part of @PHPMongoKit

php-mongo
Stand With Ukraine

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

[![Total Downloads][badge-totalDownloads-img]][badge-totalDownloads-url]
PHP ODM for MongoDB.
:star: 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 and do more things which makes your life easier.

:1234: 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 for details.
Requirements Table of contents
  • Installation
  • Common installation
  • Symfony bundle
  • Laravel
  • Yii component
  • Yii2 component
  • Support of migrations
  • Connecting
  • Mapping
  • Selecting database and collection
  • Custom collections
  • Document schema
  • Document validation
  • Getting documents by id
  • Create new document
  • Get and set data in document
  • Embedded documents
  • Get embedded document
  • Set embedded document
  • Get embedded list of documents
  • Set embedded list of documents
  • Validation of embedded documents
  • DBRefs
  • Storing document
  • Storing mapped object
  • Insert and update documents without ODM
  • Batch insert
  • Batch update
  • Moving data between collections
  • Querying documents
  • Query Builder
  • Extending Query Builder
  • Identity Map
  • Comparing queries
  • Geospatial queries
  • Fulltext search
  • Pagination
  • Persistence (Unit of Work)
  • Deleting collections and documents
  • Aggregation framework
  • Events
  • Behaviors
  • Relations
  • One-to-one relation
  • One-to-many relation
  • Many-to-many relation
  • Add relation
  • Remove relation
  • Concurency
  • Optimistic locking
  • Read preferences
  • Write concern
  • Capped collections
  • Executing commands
  • Queue
  • Migrations
  • GridFS
  • Versioning
  • Indexes
  • Caching and documents with TTL
  • Debugging
  • Logging
  • Profiling
  • Unit tests

Installation

Common installation
You can install library through Composer:
composer require sokil/php-mongo

Download latest release:
Latest sources from GitHub
Compatibility with PHP 7
PHPMongo currently based on old ext-mongo entension.
To use this ODM with PHP 7, you need to add compatibility layer, which implement API of old extension over new ext-mongodb.
To start using PHPMongo with PHP7, add requirement alcaeus/mongo-php-adapter to composer.
Restrictions for using ODM with compatibility layer you can read in known issues of original adapter.

Require adapter of old ext-mongo API to new ext-mongodb:
composer require alcaeus/mongo-php-adapter

Symfony bundle
If you use Symfony framework, you can use Symfony MongoDB Bundle which wraps this library

composer require sokil/php-mongo-bundle

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

Yii component
If you use Yii Framework, you can use Yii Adapter 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
If you use Yii2 Framework, you can use Yii2 Adapter which wraps this library

composer require phpmongokit/yii2-mongo-odm

Support of migrations
If you require migrations, you can add dependency to Migrator, based on this library:

composer require sokil/php-mongo-migrator

Connecting

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

```php