{"id":23219125,"url":"https://github.com/b2pweb/bdf-prime-mongodb","last_synced_at":"2026-04-28T18:35:39.524Z","repository":{"id":38825600,"uuid":"378109376","full_name":"b2pweb/bdf-prime-mongodb","owner":"b2pweb","description":"Bdf prime MongoDB component","archived":false,"fork":false,"pushed_at":"2023-10-19T09:10:48.000Z","size":242,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"2.1","last_synced_at":"2023-12-17T10:49:21.242Z","etag":null,"topics":["mongodb","odm"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/b2pweb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2021-06-18T10:05:50.000Z","updated_at":"2023-12-20T16:07:30.905Z","dependencies_parsed_at":"2023-12-20T16:17:49.390Z","dependency_job_id":null,"html_url":"https://github.com/b2pweb/bdf-prime-mongodb","commit_stats":{"total_commits":59,"total_committers":3,"mean_commits":"19.666666666666668","dds":"0.22033898305084743","last_synced_commit":"4ec48b377433ca96663f298d7e9592e67cb54e93"},"previous_names":[],"tags_count":7,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/b2pweb%2Fbdf-prime-mongodb","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/b2pweb%2Fbdf-prime-mongodb/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/b2pweb%2Fbdf-prime-mongodb/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/b2pweb%2Fbdf-prime-mongodb/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/b2pweb","download_url":"https://codeload.github.com/b2pweb/bdf-prime-mongodb/tar.gz/refs/heads/2.1","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247353682,"owners_count":20925325,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["mongodb","odm"],"created_at":"2024-12-18T21:19:21.577Z","updated_at":"2026-04-28T18:35:34.482Z","avatar_url":"https://github.com/b2pweb.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Prime MongoDB driver\n[![build](https://github.com/b2pweb/bdf-prime-mongodb/actions/workflows/php.yml/badge.svg)](https://github.com/b2pweb/bdf-prime-mongodb/actions/workflows/php.yml)\n[![codecov](https://codecov.io/github/b2pweb/bdf-prime-mongodb/branch/2.0/graph/badge.svg?token=VOFSPEWYKX)](https://codecov.io/github/b2pweb/bdf-prime-mongodb)\n[![Packagist Version](https://img.shields.io/packagist/v/b2pweb/bdf-prime-mongodb.svg)](https://packagist.org/packages/b2pweb/bdf-prime-mongodb)\n[![Total Downloads](https://img.shields.io/packagist/dt/b2pweb/bdf-prime-mongodb.svg)](https://packagist.org/packages/b2pweb/bdf-prime-mongodb)\n[![Type Coverage](https://shepherd.dev/github/b2pweb/bdf-prime-mongodb/coverage.svg)](https://shepherd.dev/github/b2pweb/bdf-prime-mongodb)\n\nMongoDB driver for [Prime](https://github.com/b2pweb/bdf-prime)\n\n## Installation\n\nInstall with composer :\n\n```bash\ncomposer require b2pweb/bdf-prime-mongodb\n```\n\nCreate connection :\n\n```php\n\u003c?php\nuse Bdf\\Prime\\ConnectionManager;\nuse Bdf\\Prime\\MongoDB\\Collection\\MongoCollectionLocator;\nuse Bdf\\Prime\\MongoDB\\Mongo;\n\n// declare your connexion manager\n$connections = new ConnectionManager();\n\n// Declare connection without username and password\n$connections-\u003edeclareConnection('mongo', 'mongodb://127.0.0.1/my_collection?noAuth=true');\n// With credentials\n$connections-\u003edeclareConnection('mongo', 'mongodb://user:password@127.0.0.1/my_database');\n\n// Get the connection locator\n$locator = new MongoCollectionLocator($connections);\nMongo::configure($locator); // Configure active record system\n```\n\n## Usage\n\n### Declare a document\n\nDeclare the base document class by extending `Bdf\\Prime\\MongoDB\\Document\\MongoDocument`. \nThe `_id` field is declared by this class.\n\nYou can use typed property for generate an automatic type mapping.\nUntyped fields will not be converted when retrieving from mongo.\n\n\u003e Note: it's advisable to declare all fields as nullable in case of missing field\n\n```php\n\u003c?php\n\nuse Bdf\\Prime\\MongoDB\\Document\\MongoDocument;\nuse \\MongoDB\\BSON\\Binary;\n\nclass MyDocument extends MongoDocument\n{\n    public ?string $name;\n    public ?DateTimeInterface $creationDate;\n    public ?Binary $data;\n}\n```\n\n### Declare a Mapper\n\nFor a basic usage, simply declare a mapper by extending `Bdf\\Prime\\MongoDB\\Document\\DocumentMapper`, and implementing `connection()` and `collection()` methods :\n\n```php\n\u003c?php\n\nuse Bdf\\Prime\\MongoDB\\Document\\DocumentMapper;\n\nclass MyDocumentMapper extends DocumentMapper\n{\n    /**\n     * {@inheritdoc}\n     */\n    public function connection(): string\n    {\n        // The declared connection name \n        return 'mongo';\n    }\n\n    /**\n     * {@inheritdoc}\n     */\n    public function collection(): string\n    {\n        return 'my_collection'; // The storage collection name\n    }\n}\n```\n\nMapping and fields will be automatically resolved from the document class.\n\n### Querying MongoDB\n\nThe query system use Prime interfaces, so usage is almost the same :\n\n```php\n\u003c?php\n// Get the query\n/** @var \\Bdf\\Prime\\MongoDB\\Query\\MongoQuery $query */\n$query = MyDocument::query();\n\n$query\n    -\u003ewhere('name', 'John') // Simple where works as expected\n    -\u003ewhere('value.attr', ':like', 'P%') // \"like\" operator is converted to a regex\n    -\u003ewhere('value.foo', '$type', 'javascript') // Use mongodb operator\n;\n\n// Get all documents which match with filters\n$query-\u003eall();\n\n// First returns the first matching document or null\n$query-\u003efirst();\n```\n\n### Testing\n\nUse `Bdf\\Prime\\MongoDB\\Test\\MongoTester` for create testing data.\n\n```php\n\u003c?php\n\nuse PHPUnit\\Framework\\TestCase;\nuse Bdf\\Prime\\MongoDB\\Test\\MongoTester;\n\nclass MyTest extends TestCase\n{\n    private MongoTester $tester;\n\n    protected function setUp() : void\n    {\n        $this-\u003etester = new MongoTester();\n        $this-\u003etester\n            // Declare given collections. Collections are automatically declared when `push()` on new collection\n            -\u003edeclare(FooDocument::class, BarDocument::class)\n            // Push to mongo given documents with a key for retrieve the value on test\n            -\u003epush([\n                'doc1' =\u003e new MyDocument(),\n                'doc2' =\u003e new MyDocument(),\n            ])\n        ;\n    }\n    \n    protected function tearDown() : void\n    {\n        $this-\u003etester-\u003edestroy(); // Drop all declared collections\n    }\n    \n    public function my_test()\n    {\n        $doc1 = $this-\u003etester-\u003eget('doc1'); // get document declared on setUp method\n        $this-\u003etester-\u003epush($newDoc = new FooDocument()); // Push a single document without a key (cannot be retrieved with `get()`)\n        \n        // ...\n        \n        $this-\u003eassertNotEquals($doc1, $this-\u003etester-\u003erefresh($doc1)); // Retrieve the DB version of the given document\n        $this-\u003eassertNull($newDoc, $this-\u003etester-\u003erefresh($newDoc)); // refresh can be used to check if the document exists on DB\n    }\n    \n    public function with_array_access_test()\n    {\n        // Array access syntax can also be used instead of \"classic\" method calls\n        $doc1 = $this-\u003etester['doc1']; // get document declared on setUp method\n        $this-\u003etester[] = $newDoc = new FooDocument(); // Push a single document without a key (cannot be retrieved with `get()`)\n        $this-\u003etester['doc3'] = new MyDocument(); // Push a single document with a key\n\n        // ...\n\n        $this-\u003eassertNotEquals($doc1, $this-\u003etester[$doc1]); // Retrieve the DB version of the given document\n        $this-\u003eassertTrue(isset($this-\u003etester[$newDoc])); // Check if the document exists on DB\n        \n        unset($this-\u003etester['doc3']); // Deleted a declared document\n        unset($this-\u003etester[$newDoc]); // Can also be used to delete a document without key\n\n        $this-\u003eassertFalse(isset($this-\u003etester[$newDoc])); // Document is now deleted\n    }\n}\n```\n\n### Case-insensitive search and index\n\nTo enable case-insensitive search by default, you can add default collation on table options.\nSee [Case Insensitive Indexes](https://docs.mongodb.com/manual/core/index-case-insensitive/#case-insensitive-indexes-on-collections-with-a-default-collation)\n\n```php\n\u003c?php\n\nuse Bdf\\Prime\\MongoDB\\Document\\DocumentMapper;\n\nclass MyDocumentMapper extends DocumentMapper\n{\n    /**\n     * {@inheritdoc}\n     */\n    public function connection(): string\n    {\n        // The declared connection name \n        return 'mongo';\n    }\n\n    /**\n     * {@inheritdoc}\n     */\n    public function collection(): string\n    {\n        return 'my_collection'; // The storage collection name\n    }\n    \n    /**\n     * {@inheritdoc}\n     */\n    protected function buildDefinition(\\Bdf\\Prime\\MongoDB\\Schema\\CollectionDefinitionBuilder $builder) : void\n    {\n        $builder-\u003ecollation(['locale' =\u003e 'en', 'strength' =\u003e 2]);\n    }\n}\n```\n\n### Multiple document classes\n\nMongo is schemaless, so a collection can store documents with different formats.\nYou can select a document class corresponding to DB fields by using a custom `Bdf\\Prime\\MongoDB\\Document\\Selector\\DocumentSelectorInterface`,\ndeclared using `DocumentMapper::createDocumentSelector()` :\n\n```php\n\u003c?php\n\n// Declare document classes. Note: all documents classes must inherit from a base class \nclass BaseDocument extends MongoDocument\n{\n    public ?string $_type = null; // _type is the default field used by DiscriminatorFieldDocumentSelector\n}\n\nclass FooDocument extends BaseDocument\n{\n    public ?string $_type = 'foo';\n    public ?string $foo = null;\n}\n\nclass BarDocument extends BaseDocument\n{\n    public ?string $_type = 'bar';\n    public ?string $bar = null;\n}\n\n// Declare a single mapper\nclass MyDocumentMapper extends DocumentMapper\n{\n    /**\n     * {@inheritdoc}\n     */\n    public function connection(): string\n    { \n        return 'mongo';\n    }\n\n    /**\n     * {@inheritdoc}\n     */\n    public function collection(): string\n    {\n        return 'my_collection';\n    }\n    \n    /**\n     * {@inheritdoc}\n     */\n    protected function createDocumentSelector(string $documentBaseClass): DocumentSelectorInterface\n    {\n        // Define document class mapping\n        return new DiscriminatorFieldDocumentSelector($documentBaseClass, [\n            'foo' =\u003e FooDocument::class,\n            'bar' =\u003e BarDocument::class,\n        ]);\n        \n        // If you can't introduce a field for perform discrimination, you can check fields existence :\n        return new DiscriminatorFieldDocumentSelector($documentBaseClass, [\n            FooDocument::class =\u003e ['foo'],\n            BarDocument::class =\u003e ['bar'],\n        ]);\n    }\n}\n\n// Get the base collection : it handles all document types\n$collection = BaseDocument::collection();\n\n$collection-\u003eadd(new BaseDocument(...));\n$collection-\u003eadd(new FooDocument(...));\n$collection-\u003eadd(new BarDocument(...));\n\n$collection-\u003eall(); // Return all documents from all types\n\n// Handle only \"FooDocument\" document class\n$fooCollection = FooDocument::collection();\n$fooCollection-\u003eall(); // Return only document of type \"foo\"\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fb2pweb%2Fbdf-prime-mongodb","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fb2pweb%2Fbdf-prime-mongodb","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fb2pweb%2Fbdf-prime-mongodb/lists"}