Download the PHP package phpdot/mongodb without Composer
On this page you can find all versions of the php package phpdot/mongodb. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package mongodb
phpdot/mongodb
Resilient MongoDB client for PHP 8.3+. Fluent CRUD builders, typed Document object, auto-reconnect, exception translation, and query logging. Wraps mongodb/mongodb v2.x.
Table of Contents
- Install
- Quick Start
- Architecture
- MongoConnection
- MongoConfig
- MongoConnection Lifecycle
- Topologies
- Resilience
- Database
- Collection Access
- Transactions
- Database Commands
- Collection Management
- Collection
- Insert
- Find (Quick Access)
- Find (Fluent Builder)
- Update (Fluent Builder)
- Delete (Fluent Builder)
- Replace
- Atomic Find-and-Modify
- Bulk Write
- Count and Distinct
- Aggregation
- Index Management
- Explain
- Change Streams
- Document
- Field Access
- Type Conversions
- Conversion Methods
- ArrayAccess and JsonSerializable
- Cursor
- Filter Builder
- Comparison Operators
- Array Operators
- Logical Operators
- Element Operators
- String Operators
- Geospatial Operators
- Raw Filters
- Query Logging
- GridFS
- Exception Handling
- Exception Hierarchy
- Exception Translation
- Catching Exceptions
- Escape Hatches
- API Reference
- MongoConfig API
- MongoConnection API
- Database API
- Collection API
- FindQuery API
- UpdateQuery API
- DeleteQuery API
- Document API
- Cursor API
- Filter API
- QueryLogger API
- Bucket API
- Exceptions API
- License
Install
| Requirement | Version |
|---|---|
| PHP | >= 8.3 |
| ext-mongodb | >= 2.1 |
| mongodb/mongodb | >= 2.1 |
Quick Start
Architecture
Package structure:
MongoConnection
MongoConfig
Immutable readonly DTO. All 17 parameters have sensible defaults.
Multiple hosts:
With authentication:
MongoConnection Lifecycle
Topologies
Resilience
Every collection operation runs through runWithReconnect:
- Execute the operation
- On
ConnectionExceptionor connection error code: reconnect with exponential backoff (100ms, 200ms, 400ms...), refresh the collection reference, retry once - On other errors: translate to typed exception
Recognized connection error codes: HostUnreachable (6), HostNotFound (7), NetworkTimeout (89), SocketException (9001), NotWritablePrimary (10107), InterruptedAtShutdown (11600), InterruptedDueToReplStateChange (11602), NotPrimaryNoSecondaryOk (13435), NotPrimaryOrSecondary (13436).
Database
Collection Access
Transactions
Transactions require a replica set. The callback receives the Database and a Session.
If the callback throws, the transaction is automatically aborted and the exception re-thrown.
Database Commands
Collection Management
Collection
Insert
Find (Quick Access)
Find (Fluent Builder)
find() returns a FindQuery with 16 chainable methods. Call execute(), first(), or count() to run.
Update (Fluent Builder)
updateOne() and updateMany() return an UpdateQuery.
Delete (Fluent Builder)
deleteOne() and deleteMany() return a DeleteQuery.
Replace
Replace the entire document (not a partial update).
Atomic Find-and-Modify
Find a document and atomically modify it. Returns the document before modification (by default).
Bulk Write
Execute multiple write operations in a single command.
Count and Distinct
Aggregation
PHPdot delegates aggregation building to mongodb/mongodb's Pipeline builder (46 stages, 37 accumulators, 190 expressions). PHPdot wraps execution with resilience and Document wrapping.
Index Management
Explain
Analyze query execution plans.
Change Streams
Watch for real-time changes on a collection (requires replica set).
Document
Every document returned from queries is a Document instance. Immutable, with automatic BSON-to-PHP type conversion on access.
Field Access
Type Conversions
Automatic on __get() access:
| BSON Type | PHP Type | Notes |
|---|---|---|
| UTCDateTime | DateTimeImmutable | UTC timezone |
| Nested document (associative array) | Document | Recursive — $doc->address->city |
| Array (list) | PHP array | Native foreach, count |
| Int64 | int | Native arithmetic |
| Binary | string | Via getData() |
| ObjectId | ObjectId (unchanged) | Has __toString |
| Decimal128 | Decimal128 (unchanged) | Has __toString |
| string, int, float, bool, null | unchanged | PHP natives |
toArray() converts everything to plain PHP (ObjectId and Decimal128 become strings).
Conversion Methods
ArrayAccess and JsonSerializable
Cursor
Cursor wraps MongoDB's cursor and yields Document instances. Implements IteratorAggregate.
Filter Builder
Build MongoDB query filters with a fluent API. All methods return self for chaining.
Use with builders via where():
Comparison Operators
Array Operators
Logical Operators
Element Operators
String Operators
Geospatial Operators
Raw Filters
Query Logging
Ring buffer logger with slow query tracking. Shared across all collections via Database.
Logged operations: findOne, find, insertOne, insertMany, updateOne, updateMany, deleteOne, deleteMany, replaceOne, countDocuments, estimatedDocumentCount, distinct, findOneAndUpdate, findOneAndReplace, findOneAndDelete, bulkWrite, aggregate.
GridFS
Store and retrieve large files. Wraps MongoDB's GridFS with a clean API.
Exception Handling
Exception Hierarchy
All exceptions carry the original MongoDB driver exception as getPrevious().
Exception Translation
| MongoDB Error | PHPdot Exception | Error Code |
|---|---|---|
| Duplicate key violation | DuplicateKeyException |
11000, 11001 |
| Document validation failure | ValidationException |
121 |
| Execution timeout | TimeoutException |
50 |
| MongoConnection lost after retry | ConnectionException |
6, 7, 89, 9001, ... |
| Authentication failure | AuthenticationException |
varies |
| Other write errors | WriteException |
varies |
| Other read errors | QueryException |
varies |
| Bulk write failure | BulkWriteException |
varies |
Catching Exceptions
Escape Hatches
Every wrapper exposes the underlying mongodb/mongodb object for edge cases:
API Reference
MongoConfig API
MongoConnection API
Database API
Collection API
FindQuery API
UpdateQuery API
DeleteQuery API
Document API
Cursor API
Filter API
QueryLogger API
Bucket API
Exceptions API
License
MIT
All versions of mongodb with dependencies
ext-mongodb Version ^2.1
mongodb/mongodb Version ^2.1
phpdot/contracts Version ^1.4