{"id":17491926,"url":"https://github.com/tcb13/sequel-mongo-php","last_synced_at":"2025-04-22T20:15:34.250Z","repository":{"id":47634016,"uuid":"167532425","full_name":"TCB13/sequel-mongo-php","owner":"TCB13","description":"Run SQL-like queries on MongoDB databases!","archived":false,"fork":false,"pushed_at":"2023-01-24T16:23:20.000Z","size":41,"stargazers_count":9,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-22T20:15:27.632Z","etag":null,"topics":["mongodb","mongodb-php","php","query-builder"],"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/TCB13.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}},"created_at":"2019-01-25T10:49:32.000Z","updated_at":"2024-03-21T15:23:24.000Z","dependencies_parsed_at":"2023-02-13T23:15:39.606Z","dependency_job_id":null,"html_url":"https://github.com/TCB13/sequel-mongo-php","commit_stats":null,"previous_names":[],"tags_count":31,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TCB13%2Fsequel-mongo-php","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TCB13%2Fsequel-mongo-php/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TCB13%2Fsequel-mongo-php/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/TCB13%2Fsequel-mongo-php/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/TCB13","download_url":"https://codeload.github.com/TCB13/sequel-mongo-php/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250316066,"owners_count":21410476,"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","mongodb-php","php","query-builder"],"created_at":"2024-10-19T08:06:14.593Z","updated_at":"2025-04-22T20:15:34.229Z","avatar_url":"https://github.com/TCB13.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Sequel Mongo PHP\n\nA lightweight, expressive, framework agnostic **query builder for PHP** that empowers you to run **SQL-like queries on MongoDB databases**. Enjoy the best of the two worlds!\n\n### Installation\n\nPull the package via composer.\n```shell\n$ composer require TCB13/sequel-mongo-php\n```\n\n### Usage\n\n**Use `MongoDB\\Client` to connect to your Database**:\n\n```php\n// Start a MongoDB Client to create a connection to your Database\n$mongoConnection = new \\MongoDB\\Client(\"mongodb://127.0.0.1\", [\n\t\"username\" =\u003e \"user\",\n\t\"password\" =\u003e \"pass\"\n]);\n\n/** @var \\MongoDB\\Database $mongo */\n$mongo = $mongoConnection-\u003eselectDatabase(\"DatabaseName\");\n```\n\n**Get one item from a collection**:\n\n```php\n$qb = new QueryBuilder($mongo);\n$qb-\u003ecollection(\"Users\")\n   -\u003eselect(\"_id\")\n   -\u003efind();\n$result = $qb-\u003etoObject();\nvar_dump($result);\n```\n\n**Get multiple items**:\n\n```php\n$qb = new QueryBuilder($mongo);\n$qb-\u003ecollection(\"Users\")\n   -\u003eselect(\"_id\")\n   -\u003elimit(2)\n   -\u003efindAll();\n$result = $qb-\u003etoObject();\nvar_dump($result);\n```\n\n- `select()` takes the desired fields as an `array` of strings or a variable number of parameters.\n\n**Run a complex query** with multiple `where` conditions, `limit` and `order`.\n```php\n// Use the Query Builder\n$qb = new QueryBuilder($mongo);\n$qb-\u003ecollection(\"Users\")\n   -\u003eselect(\"_id\", \"name\", \"email\", \"active\")\n   -\u003ewhere(\"setting\", \"!=\", \"other\")\n   -\u003ewhere(\"active\", false)\n   -\u003ewhere(function ($qb) {\n\t   $qb-\u003ewhere(\"isValid\", true)\n\t      -\u003eorWhere(\"dateActivated\", \"!=\", null);\n   })\n   -\u003elimit(100)\n   -\u003eorder(\"dateCreated\", \"ASC\")\n   -\u003efindAll();\n$result = $qb-\u003etoObject();\nvar_dump($result);\n```\n- If the operator is omitted in `where()` queries, `=` will be used. Eg. `-\u003ewhere(\"active\", false)`;\n- You can group `where` conditions by providing closures. Eg. `WHERE id = 1 AND (isValid = true OR dateActivated != null)` can be written as: \n```php\n-\u003ewhere(\"id\", 1)\n-\u003ewhere(function ($qb) {\n    $qb-\u003ewhere(\"isValid\", true)\n       -\u003eorWhere(\"dateActivated\", \"!=\", null);\n})\n```\n- `where()` supports the following operators `=`, `!=`, `\u003e`, `\u003e=`, `\u003c`, `\u003c=`;\n- SQL's `WHERE IN()` is also available:\n```php\n$qb = new QueryBuilder($mongo);\n\t\t$qb-\u003ecollection(\"Orders\")\n\t\t   -\u003ewhereIn(\"itemCount\", [22,4])\n\t\t   -\u003efindAll();\n\t\t$result = $qb-\u003etoArray();\n```\n- `WHERE NOT IN()` is also available via `whereNotIn()` and `orWhereNotIn()` respectively;\n\n- Examples of other useful String queries:\n```php\n-\u003ewhereStartsWith(\"item\", \"start\")\n-\u003ewhereEndsWith(\"item\", \"end\")\n-\u003ewhereContains(\"item\", \"-middle-\")\n-\u003ewhereRegex(\"item\", \".*-middle-.*\")\n```\n- For more complex String queries you may also use regex:\n```php\n-\u003ewhereRegex(\"item\", \".*-middle-.*\")\n```\n\n**Count Documents**\n\nYou may count the number of documents/records that match a query with the `count()` method:\n\n```php\n$qb = new QueryBuilder($mongo);\n$result = $qb-\u003ecollection(\"Users\")\n   -\u003ewhere(\"userid\", $this-\u003eid)\n   -\u003ecount();\nvar_dump($result);\n```\n\nNote that you may not call `find()` or `findAll()` in combination with this method.\n\n**Insert a document**:\n```php\n$qb = new QueryBuilder($mongo);\n$result = $qb-\u003ecollection(\"TestCollection\")\n             -\u003einsert([\n\t             \"item\" =\u003e \"test-insert\",\n\t             \"xpto\" =\u003e microtime(true)\n             ]);\nvar_dump($result);\n```\n- You may also insert multiple documents at once:\n```php\n$items = [\n\t[\n\t\t\"item\" =\u003e \"test-insert-multi\",\n\t\t\"xpto\" =\u003e microtime(true)\n\t],\n\t[\n\t\t\"item\" =\u003e \"test-insert-multi\",\n\t\t\"xpto\" =\u003e microtime(true)\n\t]\n];\n\n$qb     = new QueryBuilder($mongo);\n$result = $qb-\u003ecollection(\"TestCollection\")\n             -\u003einsert($items);\n```\n\n**Delete a Document**:\n```php\n$qb     = new QueryBuilder($mongo);\n$result = $qb-\u003ecollection(\"TestCollection\")\n             -\u003ewhereStartsWith(\"item\", \"test-insert-\")\n             -\u003edelete();\nvar_dump($result);\n```\n\n**Update a Document**:\n```php\n// Update item\n$qb     = new QueryBuilder($mongo);\n$result = $qb-\u003ecollection($collection)\n             -\u003ewhere(\"_id\", new MongoID(\"51ee74e944670a09028d4fc9\"))\n             -\u003eupdate([\n\t             \"item\" =\u003e \"updated-value \" . microtime(true)\n             ]);\nvar_dump($result);\n```\n- You may update only a few fields or an entire document - like like an SQL `update` statement.\n\n**Join Collections**:\n\n_**join($collectionToJoin, $localField, $operatorOrForeignField, $foreignField)**_\n```php\n$qb = new QueryBuilder($mongo);\n$qb-\u003ecollection(\"Orders\")\n    //-\u003eselect(\"_id\", \"products#joined.sku\")\n    //-\u003ejoin([\"products\" =\u003e \"products#joined\"], \"sku\", \"=\", \"item\"])\n    //-\u003ejoin(\"products\", \"sku\", \"=\", \"item\")\n   -\u003ejoin(\"Products\", \"sku\", \"item\")\n   -\u003efindAll();\n$result = $qb-\u003etoArray();\nvar_dump($result);\n```\n\n**Special Functions**:\n\n*Max(string $property, ?string $alias = null)* - get the maximum value in a set of values:\n```php\n$qb = new QueryBuilder($mongo);\n$qb-\u003ecollection(\"Orders\")\n   -\u003eselect(\"id\", new Max(\"datecreated\", \"lastorder\"))\n   -\u003ewhere(\"userid\", \"u123\")\n   -\u003efind();\n$result = $qb-\u003etoArray();\nvar_dump($result);\n```\n*Min(string $property, ?string $alias = null)* - get the minimum value in a set of values:\n```php\n$qb = new QueryBuilder($mongo);\n$qb-\u003ecollection(\"Orders\")\n   -\u003eselect(\"id\", new Min(\"datecreated\", \"lastorder\"))\n   -\u003ewhere(\"userid\", \"u123\")\n   -\u003efind();\n$result = $qb-\u003etoArray();\nvar_dump($result);\n```                \n*Increment(string $propertyName, int $incrementBy = 1)* - increment or decrement a document property by a value:\n```php\n$qb = new QueryBuilder($mongo);\n$qb-\u003ecollection(\"Orders\")\n   -\u003ewhere(\"id\", 12345)\n   -\u003eupdate([\n    \tnew Increment(\"status\")\n    ]);\n```\n\n*ArrayContains(string $arrayProperty, $needles)* - check if an array in a document contains a value (or at least one value if an array is passed):\n```php\n$qb = new QueryBuilder($mongo);\n$qb-\u003ecollection(\"Orders\")\n   -\u003eselect(\"id\")\n   -\u003ewhere(new ArrayContains(\"prioritaryItems\", \"123\"))\n   -\u003efindAll();\n$result = $qb-\u003etoArray();\nvar_dump($result);\n```\n*ArrayLength(string $arrayProperty, ?string $alias = null)* - get the length of an array:\n```php\n$qb = new QueryBuilder($mongo);\n$qb-\u003ecollection(\"Orders\")\n   -\u003eselect(\"id\", new ArrayLength(\"prioritaryItems\", \"prioritaryItems_lenght\"))\n   -\u003ewhere(\"prioritaryItems_lenght\", \"\u003e\", 0)\n   -\u003efindAll();\n$result = $qb-\u003etoArray();\nvar_dump($result);\n```\n*ArrayPush(string $arrayProperty, mixed $value)* - add an element to an array. Example: document with a `tokens` property that is an array:\n```php\n$qb = new QueryBuilder($mongo);\n$qb-\u003ecollection(\"Users\")\n   -\u003ewhere(\"id\", 123)\n   -\u003eupdate([\n        new ArrayPush(\"tokens\", \"...\")\n    ]);\n$result = $qb-\u003etoArray();\nvar_dump($result);\n```\n*ArrayPull(string $arrayProperty, mixed $value)* - remove an element from an array. Example: document with a `tokens` property that is an array:\n```php\n$qb = new QueryBuilder($mongo);\n$qb-\u003ecollection(\"Users\")\n   -\u003ewhere(\"id\", 123)\n   -\u003eupdate([\n        new ArrayPull(\"tokens\", \"...\")\n    ]);\n$result = $qb-\u003etoArray();\nvar_dump($result);\n```\n**Debug Queries**:\n\nIt is possible possible to debug the query pipeline built by the Query Builder for each query.\n```php\nQueryBuilder::$pipelineDebug = true; // Enable pipeline debugging!\n\n// Run a query\n$result = (new QueryBuilder())-\u003ecollection(\"xyz\")\n            -\u003ewhere(\"active\", true)\n            -\u003efindAll()\n            -\u003etoArray();\n\n// Fetch the pipeline built by the Query Builder\nvar_dump(QueryBuilder::getLastPipelineLog()); // Get the pipeline built for the last query\nvar_dump(QueryBuilder::getPipelineLogs()); // Get all pipelines ever built by the query builder\n```\n\n**For more examples check out the `examples` directory.**\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftcb13%2Fsequel-mongo-php","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftcb13%2Fsequel-mongo-php","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftcb13%2Fsequel-mongo-php/lists"}