Skip to content

muhammetsafak/MongoPHP

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MongoPHP

A slightly easier way to use MongoDB with PHP. This library aims to simplify CRUD operations on MongoDB with PHP.

Note : If you need a more comprehensive library, you can use the official mongodb/mongodb library.

Requirements

Installation

composer require muhammetsafak/mongophp

Usage

Connection

require_once "vendor/autoload.php";
use MuhammetSafak\MongoPHP\MongoPHP;

$db = new MongoPHP('mongodb://127.0.0.1:27017', 'databaseName');

Create (Insert)

Single Insert :

/** @var $db \MuhammetSafak\MongoPHP\MongoPHP */
$res = $db->insert(['user' => 'muhammet', 'mail' => '[email protected]'])
            ->save('userCollection');
            
if($res){
    echo 'Ok!';
}else{
    foreach ($db->getErrors() as $err) {
        echo 'Error: ' . $err . \PHP_EOL;
    }
}

Multi Insert :

/** @var $db \MuhammetSafak\MongoPHP\MongoPHP */
$res = $db->insert(['user' => 'muhammet', 'mail' => '[email protected]'])
            ->insert(['user' => 'ahmet', 'mail' => '[email protected]'])
            ->save('userCollection');
            
if($res){
    echo 'Ok!';
}else{
    foreach ($db->getErrors() as $err) {
        echo 'Error: ' . $err . \PHP_EOL;
    }
}

Read

/** @var $db \MuhammetSafak\MongoPHP\MongoPHP */
$res = $db->read('userCollection', ['mail' => '[email protected]']);
foreach ($res as $row) {
    echo '#' . $row->_id . ': ' . $row->user . ' <' . $row->mail . '>' . \PHP_EOL;
}

Update

Note : This will replace the entire row with the new data, not just the specified column.

/** @var $db \MuhammetSafak\MongoPHP\MongoPHP */
$res = $db->update(['user' => 'old_user_name'], ['user' => 'new_username'])
            ->save('userCollection');
            
if($res){
    echo 'Ok!';
}else{
    foreach ($db->getErrors() as $err) {
        echo 'Error: ' . $err . \PHP_EOL;
    }
}

Delete

/** @var $db \MuhammetSafak\MongoPHP\MongoPHP */
$res = $db->delete(['user' => 'muhammet'])
            ->save('userCollection');
            
if($res){
    echo 'Ok!';
}else{
    foreach ($db->getErrors() as $err) {
        echo 'Error: ' . $err . \PHP_EOL;
    }
}

Getting Help

If you have questions, concerns, bug reports, etc, please file an issue in this repository's Issue Tracker.

Contributing

All contributions to this project will be published under the MIT License. By submitting a pull request or filing a bug, issue, or feature request, you are agreeing to comply with this waiver of copyright interest.

  • Fork it ( https://github.com/muhammetsafak/mongophp/fork )
  • Create your feature branch (git checkout -b my-new-feature)
  • Commit your changes (git commit -am "Add some feature")
  • Push to the branch (git push origin my-new-feature)
  • Create a new Pull Request

Credits

License

Copyright © 2022 MIT License