Monga
A simple class for working with MongoDB. Those new to Mongo, should go through
the SQL to Mongo Mapping Chart, (*1)
### Setup
Add a composer.json file to your project:, (*2)
{
"require": {
"rcastera/mongo": "v1.0.0"
}
}
Then provided you have composer installed, you can run the following command:, (*3)
$ composer.phar install
That will fetch the library and its dependencies inside your vendor folder. Then you can add the following to your
.php files in order to use the library (if you don't already have one)., (*4)
require 'vendor/autoload.php';
Then you need to use the relevant class, and instantiate the class. For example:, (*5)
### Getting Started
require 'vendor/autoload.php';
use rcastera\Database\Mongo\Monga;
$mongo = new Monga();
### Examples
setDatabase('test')->setCollection('contacts')->getCollection()->find()->limit(10); ?>
hasNext()): ?>
hasNext()): ?>
getNext(); ?>
No contacts found., (*6)
Find all documents and sort by name in ascending order.
setDatabase('test')->setCollection('contacts')->getCollection()->find()->sort(array('name' => 1)); ?>
hasNext()): ?>
hasNext()): ?>
getNext(); ?>
No contacts found., (*7)
Find all documents where name = 'Richard Castera'.
setDatabase('test')->setCollection('contacts')->getCollection()->find(array('name' => 'Richard Castera')); ?>
hasNext()): ?>
hasNext()): ?>
getNext(); ?>
No contacts found., (*8)
Find where name = 'Richard Castera' and update address.
'Richard Castera'
);
$updates = array(
'$set' => array(
'address' => '2 Smith Lane'
)
);
$options = array(
'upsert' => false
);
?>
setDatabase('test')->setCollection('contacts')->getCollection()->update($criteria, $updates, $options); ?>
Contact updated., (*9)
Contact not updated., (*10)
Insert new document.
'Isabella Castera',
'age' => '3',
'address' => '3 Smith Lane'
);
?>
setDatabase('test')->setCollection('contacts')->getCollection()->insert($document); ?>
Contact inserted., (*11)
Contact not inserted., (*12)
Delete a document.
'Elisabeth Castera'
);
$options = array(
'justOne' => TRUE
);
?>
setDatabase('test')->setCollection('contacts')->getCollection()->delete($criteria, $options); ?>
Contact deleted., (*13)
Contact not deleted., (*14)
### Contributing
- Fork it.
- Create a branch (
git checkout -b my_branch)
- Commit your changes (
git commit -am "Added something")
- Push to the branch (
git push origin my_branch)
- Create an Issue with a link to your branch
- Enjoy a refreshing Coke and wait