wizbii/mongo-bundle
Symfony bundle for MongoDB and Doctrine ODM that adds repository helpers, query and aggregation builders, pagination, and filtering so PHP applications can model, query, and paginate MongoDB documents with less boilerplate. It streamlines configuration and document mapping.
composer require wizbii/mongo-bundle
# ensure the PHP extension is present for your platform
pecl install mongodb
config/bundles.php:
Wizbii\MongoBundle\WizbiiMongoBundle::class => ['all' => true],
config/packages/wizbii_mongo.yaml (verify the exact tree against DependencyInjection/Configuration.php in the repo — the config shape is the first place to look):
wizbii_mongo:
uri: '%env(MONGODB_URI)%'
database: '%env(MONGODB_DATABASE)%'
Resources/config/services.xml (or .yaml) and the bundle class. That file tells you the service IDs, whether anything is autowired, and whether you get a raw MongoDB\Client / MongoDB\Database or a thin manager wrapper. This is a small bundle — it is not a full ODM like Doctrine MongoDB, so expect to work with the native mongodb/mongodb library objects.public function __construct(private \MongoDB\Database $db) {}
public function ping(): array
{
$this->db->selectCollection('health')->insertOne(['at' => new \MongoDB\BSON\DateTimeImmutable()]);
return $this->db->selectCollection('health')->findOne([], ['sort' => ['at' => -1]]);
}
If autowiring fails, fall back to the explicit service id from the config file (e.g. wizbii_mongo.database or similar).MongoDB\Client/Database injected into services. Persist and query with the native mongodb/mongodb API; don't expect repositories, identity maps, or change tracking.selectCollection() directly:
final class OrdersCollection
{
public function __construct(private \MongoDB\Database $db) {}
public function collection(): \MongoDB\Collection { return $this->db->selectCollection('orders'); }
}
This is the cleanest way to keep BSON specifics out of controllers and Messenger handlers.app, analytics, sessions) and inject with @wizbii_mongo.database.app. Aliasing the default is usually a one-liner in your services.yaml.upsert keyed by a natural id or a _id you generate up front).docker compose in CI (best for index and query correctness), or replace the injected Database/Client with a stub in unit tests. Because the bundle exposes plain library objects, mocking is straightforward — you don't have to fake a UnitOfWork.createIndex / createIndexes from a console command or a fixture loader at deploy time, not on the hot path.Configuration.php and the bundle's extension class first — it is faster than debugging a missing-service error.mongodb extension required. Without it, mongodb/mongodb installs but fails at runtime. Confirm php -m | grep mongodb on every environment including CI.ObjectId, UTCDateTime, Decimal128, and Binary do not round-trip as plain strings. Store dates as UTCDateTime (not DateTime) unless you have a conversion layer; store ids as ObjectId unless you have a deliberate reason to use strings.connectTimeoutMS — the default can be long enough to blow up request latency.mongod, startSession() / transaction attempts will throw. Verify the deployment topology before relying on multi-document atomicity.@, :, or / in a MONGODB_URI will be mis-parsed. Either URL-encode or use the separate host/user/password config keys if the bundle supports them.mongodb.debug ini setting), server-side slow query logs, and explain() for the queries that matter. Build a small helper command that prints collection stats and index usage.MongoDB\Client and configure anything (read preference, write concern, driver options) by hand. Don't fight the bundle if it lacks an option — build the client yourself and register it as a service.composer.json and be prepared to fork or replace. Keep the usage surface small (a handful of repository services) so swapping the bundle for a hand-rolled services.yaml block is a half-day job, not a week.How can I help you explore Laravel packages today?