<commit_before>---
layout: post
title: Introducing MongoQB
permalink: /2012/11/introducing-mongoqb/
---

A few years ago I released a [MongoDB library](https://github.com/alexbilbie/codeigniter-mongodb-library) for the [CodeIgniter](http://codeigniter.com/) PHP framework which has become quite popular thanks to it's likeness to CodeIgniter's query builder library.

I've just spent the last week travelling and to keep myself occupied I spent some time re-architecting the library's code; removing the framework dependancy, making it [Composer](http://getcomposer.org/) friendly and adding a full suite of unit tests.

## Install via Packagist and Composer

Add the following into your composer.json file:

{% highlight javascript %}
{
	"require": {
		"alexbilbie/mongoqb": "*"
	}
}
{% endhighlight %}

Then run

{% highlight bash %}
composer install
{% endhighlight %}

## Install via Git

{% highlight bash %}
git clone git://git@github.com:alexbilbie/MongoQB
{% endhighlight %}

## Download a zip/tarball

Download the latest version:

* [zip](https://github.com/alexbilbie/MongoQB/archive/master.zip)
* [tar](https://github.com/alexbilbie/MongoQB/archive/master.tar.gz)

(Note the zip/tarball won't include any unit tests or composer files)

## Unit tests

To run the unit test suite make sure you have MongoDB installed locally and running with no authentication and on the default port - 27017.

Then run:

{% highlight bash %}
composer update --dev
cd vendor/alexbilbie/mongoqb
phpunit -c tests/phpunit.xml
{% endhighlight %}

## Example usage

### Connect to the database

{% highlight php %}
<?php
$qb = \MongoQB\Builder(array(
	'dsn'	=>	'mongodb://user:pass@localhost:27017/databaseName'
);
{% endhighlight %}

### Insert a document

{% highlight php %}
<?php
$qb->insert('collectionName', [
	'name'	=>	'Alex',
	'age'		=>	22,
	'likes'	=>	['whisky', 'gin']
]);
{% endhighlight %}

### Update a single document

{% highlight php %}
<?php
$qb
	->where(['name' => 'Alex'])
	->set([
		'country' => 'UK',
		'job'	=>	'Developer'
	])
	->push('likes', ['PHP', 'coffee'])
	->update('collectionName');
{% endhighlight %}

### Delete a single document

{% highlight php %}
<?php
$qb
	->where(['name' => 'Alex'])
	->delete('collectionName');
{% endhighlight %}

### Search for matching documents

{% highlight php %}
<?php
$results = $qb
	->whereGt('age', 21)
	->whereIn('likes', ['whisky'])
	->where('country', 'UK')
	->get('collectionName');
{% endhighlight %}

If you find any bugs please file a report in the [Issue tracker](https://github.com/alexbilbie/MongoQB/Issues)<commit_msg>Small updates<commit_after>---
layout: post
title: Introducing MongoQB
permalink: /2012/11/introducing-mongoqb/
---

A few years ago I released a [MongoDB library](https://github.com/alexbilbie/codeigniter-mongodb-library) for the [CodeIgniter](http://codeigniter.com/) PHP framework which has become quite popular thanks to it's likeness to CodeIgniter's query builder library.

I've just spent the last week travelling and to keep myself occupied I spent some time re-architecting the library's code; removing the framework dependancy, making it [Composer](http://getcomposer.org/) friendly and adding a full suite of unit tests.

## Install via Packagist and Composer

Add the following into your composer.json file:

{% highlight javascript %}
{
	"require": {
		"alexbilbie/mongoqb": "*"
	}
}
{% endhighlight %}

Then run

{% highlight bash %}
composer install
{% endhighlight %}

## Install via Git

{% highlight bash %}
git clone git://git@github.com:alexbilbie/MongoQB
{% endhighlight %}

## Download a zip/tarball

Download the latest version:

* [zip](https://github.com/alexbilbie/MongoQB/archive/master.zip)
* [tar](https://github.com/alexbilbie/MongoQB/archive/master.tar.gz)

(Note the zip/tarball won't include any unit tests or composer files)

## Unit tests

To run the unit test suite make sure you have MongoDB installed locally and running with no authentication and on the default port - 27017. The library currently has 100% unit test coverage.

Then run:

{% highlight bash %}
composer update --dev
cd vendor/alexbilbie/mongoqb
phpunit -c tests/phpunit.xml
{% endhighlight %}

## Example usage

### Connect to the database

{% highlight php %}
<?php
$qb = \MongoQB\Builder(array(
	'dsn'	=>	'mongodb://user:pass@localhost:27017/databaseName'
);
{% endhighlight %}

### Insert a document

{% highlight php %}
<?php
$qb->insert('collectionName', [
	'name'	=>	'Alex',
	'age'	=>	22,
	'likes'	=>	['whisky', 'gin']
]);
{% endhighlight %}

### Update a single document

{% highlight php %}
<?php
$qb
	->where(['name' => 'Alex'])
	->set([
		'country' => 'UK',
		'job' => 'Developer'
	])
	->push('likes', ['PHP', 'coffee'])
	->update('collectionName');
{% endhighlight %}

### Delete a single document

{% highlight php %}
<?php
$qb
	->where(['name' => 'Alex'])
	->delete('collectionName');
{% endhighlight %}

### Search for matching documents

{% highlight php %}
<?php
$results = $qb
	->whereGt('age', 21)
	->whereIn('likes', ['whisky'])
	->where('country', 'UK')
	->get('collectionName');
{% endhighlight %}

If you find any bugs please file a report in the [Issue tracker](https://github.com/alexbilbie/MongoQB/Issues)
{'fraction_non_alphanumeric': 0.15632183908045977, 'fraction_numerical': 0.008237547892720307, 'mean_word_length': 4.601931330472103, 'pattern_counts': {'":': 4, '<': 13, '<?xml version=': 0, '>': 41, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5094509', 'n_tokens_mistral': 1872, 'n_tokens_neox': 1673, 'n_words': 518}