Giter Club home page Giter Club logo

mongophp's Introduction

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

mongophp's People

Contributors

muhammetsafak avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.