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
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
Common installation
You can install library through Composer:
Download latest release:
Latest sources from GitHub
Compatibility with PHP 7
Symfony bundle
If you use Symfony framework, you can use Symfony MongoDB Bundle which wraps this library
Laravel
If you use Laravel framework, use [Laravel adapter](https://github.com/sokil/laravel-mongo-odm )
Yii component
If you use Yii Framework, you can use Yii Adapter which wraps this library
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
Support of migrations
If you require migrations, you can add dependency to Migrator, based on this library:
Connecting
Single connection
Connecting to MongoDB server made through
```php
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
- 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 0.9 or above (Some features require >= 1.5)
- PHP 7
- PHP MongoDB Extension 1.0 or above
- Compatibility layer. Please, note some restriontions
- HHVM Driver not supported.
- PHP-Mongo v.2 (Currentry is under development and can not be used in production)
- PHP 7
- PHP MongoDB Extension natively without legacy driver adapters.
- PHP 5 not supported
- HHVM Driver not supported.
- 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
Common installation
You can install library through Composer:
composer require sokil/php-mongoDownload latest release:
Latest sources from GitHub
Compatibility with PHP 7
PHPMongo currently based on old ext-mongo entension.Require adapter of old
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.
ext-mongo API to new ext-mongodb:
composer require alcaeus/mongo-php-adapterSymfony bundle
If you use Symfony framework, you can use Symfony MongoDB Bundle which wraps this library
composer require sokil/php-mongo-bundleLaravel
If you use Laravel framework, use [Laravel adapter](https://github.com/sokil/laravel-mongo-odm )
composer require phpmongokit/laravel-mongo-odmYii component
If you use Yii Framework, you can use Yii Adapter which wraps this library
composer require sokil/php-mongo-yiiThis 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-odmSupport of migrations
If you require migrations, you can add dependency to Migrator, based on this library:
composer require sokil/php-mongo-migratorConnecting
Single connection
Connecting to MongoDB server made through
\Sokil\Mongo\Client class:```php