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.
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()
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()
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()
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()
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()
Retrieves the ID of the last inserted document.
return - ID of the last inserted record
count()
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()
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()
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()
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
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.