# MongodbDriver

The `MongoDBDriver.php` file, located in the `Drivers/Database/` directory of the PHPFast framework, serves as an interface for seamless interaction with a MongoDB database. It offers essential methods for establishing connections, executing queries, and managing data efficiently. Below is a comprehensive breakdown of each method, complete with usage examples.

## Configuaration

```php
'db' => [
        'db_driver'   => 'mongodb',
        'db_host'     => 'localhost',
        'db_port'     => 3306,
        'db_username' => 'root',
        'db_password' => '',
        'db_database' => 'cms.vn',
        'db_charset'  => 'utf8mb4',
        'db_collate'  => 'utf8mb4_unicode_ci',
    ],
```

## `__construct()`

```php
<?php
public function __construct($config) {
    $this->config = $config;
    $this->connect();
}
```

Initializes the `MongoDBDriver` object with the provided configuration and establishes a connection to the MongoDB database.

* `$config`: An array containing the MongoDB configuration settings.

## `query()`

```php
<?php
public function query($command, $params = []) {}
```

Executes a query on a specified collection with optional filters and options.

* `$command` - An array of MongoDB commands
* `$params` - Optional parameters.
* `return` - Result of the query

## `insert()`

```php
public function insert($collection, $data) {}
```

Inserts a new document into a specified collection.

* `$collection`: The name of the collection.
* `$data`: An array representing the document to insert.
* `return` - Returns true if the data is successfully inserted, otherwise false.

## `update()`

```php
<?php
public function update($collection, $data, $where, $params = []) {}
```

Updates documents in a specified collection that match the filter criteria.

* `$collection` - the name of the collection.
* `$data` - an array of data to update.
* `$where` - optional filter conditions.
* `$params` - optional parameters for the filter.
* `return` - Returns true if the update is successful, otherwise false

## `delete()`

```php
<?php
public function delete($collection, $where, $params = []) {}
```

Deletes documents from a specified collection that match the filter criteria.

* `$collection` - the name of the collection.
* `$where` - optional filter conditions.
* `$params` - optional parameters for the filter.
* `return` - Returns true if the deletion is successful, otherwise false

## `lastInsertId()`

```php
<?php
public function lastInsertId() {
    return $this->lastInsertedId ?? null;
}
```

Retrieves the ID of the last inserted document.

`return` - ID of the last inserted record

## `count()`

```php
public function count($collection, $where = '', $params = []) {}
```

Counts the number of documents in a collection

* `$collection` - the name of the collection&#x20;
* `$where` - optional filter conditions
* `$params` - optional parameters for the filter.
* `return` - Number of records in the collection

Example:

## `fetchAll()`

```php
<?php
public function fetchAll($collection, $where = '', $params = [], $orderBy = '', $page = 1, $limit = null) {}
```

Executes a SELECT query to retrieve multiple documents.

* `$collection`: The name of the collection.
* `$where`: The WHERE clause to filter documents (optional).
* `$params`: An array of values corresponding to the parameters in the WHERE clause (optional).
* `$orderBy`: The ORDER BY clause (optional).
* `$page`: The current page number (optional).
* `$limit`: The number of results to limit (optional).
* `return` - Array containing the query results

## `fetchPagination()`

```php
<?php
public function fetchPagination($collection, $where = '', $params = [], $orderBy = '', $page = 1, $limit = null) {}
```

Executes a SELECT query to retrieve multiple documents with pagination support

* `$collection`: The name of the collection.
* `$where`: The WHERE clause to filter documents (optional).
* `$params`: An array of values corresponding to the parameters in the WHERE clause (optional).
* `$orderBy`: The ORDER BY clause (optional).
* `$page`: The current page number (optional).
* `$limit`: The number of results per page (optional).
* `return` - Query results and information about whether there is a next page

## `fetchRow()`

```php
<?php
public function fetchRow($collection, $where = '', $params = [], $orderBy = '', $page = 1) {}
```

Executes a SELECT query to retrieve a single document.

* `$collection`: The name of the collection.
* `$where`: The WHERE clause to filter documents.
* `$params`: An array of values corresponding to the parameters in the WHERE clause.
* `$orderBy`: The ORDER BY clause (optional).
* `$page`: The current page number (optional).
* `return` - Array containing the query result or null if no result is found

{% hint style="info" %}
Summary

The `MongoDBDriver.php` file provides methods to interact with a MongoDB database, including connecting to the database, executing queries, and managing data. This implementation allows developers to perform MongoDB operations in their **PHPFast** applications efficiently.
{% endhint %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.cmsfullform.com/documents/drivers/database/mongodbdriver.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
