PHPackages                             jasny/db-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. jasny/db-mongo

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

jasny/db-mongo
==============

Adds modern OOP DB patterns to the Mongo PHP extension

v1.3.2(6y ago)519.2k↓68.6%4[2 issues](https://github.com/jasny/db-mongo/issues)MITPHPPHP &gt;=7.0

Since Oct 7Pushed 5y agoCompare

[ Source](https://github.com/jasny/db-mongo)[ Packagist](https://packagist.org/packages/jasny/db-mongo)[ Docs](http://jasny.github.com/db-mongo)[ RSS](/packages/jasny-db-mongo/feed)WikiDiscussions v1.x-dev Synced 1mo ago

READMEChangelog (3)Dependencies (4)Versions (29)Used By (0)

Jasny DB Mongo
==============

[](#jasny-db-mongo)

[![Build Status](https://camo.githubusercontent.com/6cb83ecc34d0ec96cf45265aa7b38d250e045dd873c9a1a431aa1c8a878d5c5d/68747470733a2f2f7365637572652e7472617669732d63692e6f72672f6a61736e792f64622d6d6f6e676f2e706e673f6272616e63683d6d6173746572)](http://travis-ci.org/jasny/db-mongo)

Jasny DB Mongo adds OOP design patterns to PHP's [MongoDB extension](https://php.net/manual/en/set.mongodb.php).

- [Named connections](#named-connections)
- [Entity](#entity)
- [Document object](#document-object)
- [Data mapper](#data-mapper)
- [Indexing](#indexing)
- [Metadata](#metadata)
- [Type casting](#type-casting)
- [Validation](#validation)
- [Lazy loading](#lazy-loading)
- [Soft deletion](#soft-deletion)
- [Resultset](#resultset)
- [Maintainable code](#maintainable-code)

Jasny DB Mongo is not a DB abstraction layer, it **extends** the Mongo classes (except for `Jasny\DB\Mongo\Cursor` class, which incapsulates `MongoDB\Driver\Cursor`, but still allowing to call it's methods transparently). All of Mongo's classes, properties and methods are available and will work as described in the PHP manual.

### Installation

[](#installation)

Use [composer](https://getcomposer.org/) to install Jasny Mongo DB.

```

php composer.php require jasny/db-mongo '~2.0'

```

### Usage

[](#usage)

It can be used in the same way as MongoDB extension, with a few improvements.

#### **Init connection**

[](#init-connection)

When creating Database instance, we can use not only `MongoDB\Driver\Manager` as first parameter, but also an array of options, or even a uri connection string:

```

$options = [
    'client' => 'mongodb://localhost:27017',
    'database' => 'test'
];

$db = new DB($options, '');

```

or

```

$uri = 'mongodb://user:password@test-host:27017/test-db?foo=bar';
$db = new DB($uri, '');

```

Or database name can be passed as second parameter, like it is required in `MongoDB\Database`.

#### **Saving items**

[](#saving-items)

You can use collection save methods, defined in `MongoDB\Collection`. Unlike in old [PHP Mongo](https://php.net/manual/en/book.mongo.php) extension, `save` method is removed from new extension. Our library implements it in `Jasny\DB\Mongo\Collection` class, as it is very handy.

So you can do:

```

$collection = $db->test_collection;
$document = ['foo' => 'bar'];

$collection->save($document);

```

After this `$document` will contain `_id` field.

If you use other saving methods like `replaceOne`, `insertOne`, `insertMany`, id field is not automatically attached to document, as we need to follow parent methods declaration, which does not allow this.

To assign created id to document, you can do:

```

$result = $collection->insertOne($document);
$collection->useResultId($document, '_id', $result);

```

That is automatically performed in some of our library classes, like `Jasny\DB\Mongo\DataMapper` or `Jasny\DB\Mongo\Document`.

#### **Fetching from db**

[](#fetching-from-db)

When using `$cursor = $collection->find($filter)` for fetching records, an instance of `Jasny\DB\Mongo\Cursor` is returned. It does not extend `MongoDB\Driver\Cursor` class, as all `MongoDB\Driver` classes are final. Instead it incapsulates it, implementing magical calling of all it's methods.

So the following check won't work:

```

$cursor instanceof MongoDB\Driver\Cursor; // false

```

But you still can do everything else:

```

$asArray = $cursor->asArray();

```

or performing `foreach` iteration over cursor.

#### **Cast fetched records**

[](#cast-fetched-records)

There is a base class we use for items, that are stored in DB. That's `Jasny\DB\Entity`, that is defined in [Jasny DB](https://github.com/jasny/db) repository.

Fetching db records using collection methods `find` and `findOne` can produce records of this class (or any of it subclasses you define).

To make use of that, you should obtain collection instance in the following way:

```

$collection = $db->selectCollection('foo_collection', ['documentClass' => SomeEntity::class]);

```

Casting to `Entity` class is performed by our framework, without use of `MongoDB` `typeMap` functionality, because casting can be pretty complex.

Than in the following case:

```

$record = $collection->findOne($filter);

```

`$record` is an instance of `Jasny\DB\Entity`.

In the following case:

```

$cursor = $collection->find($filter);

```

to obtain casted records, you should either iterate over `$cursor` using `foreach`, or use `$cursor->toArrayCast()` method, implemented by our framework. If you use native `$cursor->toArray()` method, implemented by `MongoDB\Driver\Cursor`, records use casting defined in collection's `typeMap` option. By default we set this option to use casting to array.

###  Health Score

35

—

LowBetter than 80% of packages

Maintenance12

Infrequent updates — may be unmaintained

Popularity30

Limited adoption so far

Community13

Small or concentrated contributor base

Maturity69

Established project with proven stability

 Bus Factor1

Top contributor holds 62.6% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~70 days

Recently: every ~268 days

Total

27

Last Release

2405d ago

Major Versions

v0.2.1 → v1.0.02015-09-17

PHP version history (3 changes)v0.1.0PHP &gt;=5.4.0

v1.2.5PHP &gt;=5.6.0

v1.3.2PHP &gt;=7.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/3379a93d51305df325df9045e1a8b205d195e4e8c01312dff53a000ee79002eb?d=identicon)[jasny](/maintainers/jasny)

---

Top Contributors

[![jasny](https://avatars.githubusercontent.com/u/100821?v=4)](https://github.com/jasny "jasny (114 commits)")[![Minstel](https://avatars.githubusercontent.com/u/6154708?v=4)](https://github.com/Minstel "Minstel (64 commits)")[![moesjarraf](https://avatars.githubusercontent.com/u/5793511?v=4)](https://github.com/moesjarraf "moesjarraf (3 commits)")[![svenstm](https://avatars.githubusercontent.com/u/1632578?v=4)](https://github.com/svenstm "svenstm (1 commits)")

---

Tags

database-abstractionmongodbphp7databasedbmongodbmongo

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/jasny-db-mongo/health.svg)

```
[![Health](https://phpackages.com/badges/jasny-db-mongo/health.svg)](https://phpackages.com/packages/jasny-db-mongo)
```

###  Alternatives

[mongodb/laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel

7.1k7.2M71](/packages/mongodb-laravel-mongodb)[moloquent/moloquent

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)

120114.6k7](/packages/moloquent-moloquent)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
