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
'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()
__construct()
<?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()
query()
<?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()
insert()
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()
update()
<?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()
delete()
<?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()
lastInsertId()
<?php
public function lastInsertId() {
return $this->lastInsertedId ?? null;
}
Retrieves the ID of the last inserted document.
return
- ID of the last inserted record
count()
count()
public function count($collection, $where = '', $params = []) {}
Counts the number of documents in a collection
$collection
- the name of the collection$where
- optional filter conditions$params
- optional parameters for the filter.return
- Number of records in the collection
Example:
fetchAll()
fetchAll()
<?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()
fetchPagination()
<?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()
fetchRow()
<?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
Last updated
Was this helpful?