# Installation

Use npm command in order to install bh-mongo module:

```bash
npm install --save bh-mongo
```

# Use mongodb with NodeJs

The bh-mongo class allows to manage a mongodb connection inside a NodeJs project. It implements the methods required for the BH unified handler compliance:

  - connect(settings) : create a new connection to a mongo server. Settings can be a bh-section object or a path to a configuration file. If settings is not specified a default configuration file will be used (./conf/mysql.conf.json). Connect returns a promise.

  Settings or configuration file format:

  ```json
  {
    "host" : "mongodb://localhost:27017/test"
  }
  ```

  - close()          : close the current connection to mysql. Close returns a promise.
  - query(settings)  : execute a query described by settings in the current connection. Query returns a promise.

  Settings format:

  ```json
  {
    "collection" : "test",
    "type" : "select",
    "search" : {
      "value" : {
        "$eq" : 1
      }
    }
  }
  ```

  ```json
  {
    "collection" : "test",
    "type" : "insert",
    "args" : [
      {
        "value" : 1
      }
    ]
  }
  ```

  - transaction()    : do nothing, transactions are not supported in mongodb. Transaction returns a promise.
  - inTransaction()  : return always false.
  - commit()         : do nothing, commits are not supported in mongodb. Commit returns a promise.
  - rollback()       : do nothing, rollbacks are not supported in mongodb. Rollback returns a promise.

# Usage example:

```js
var MongoClass = require('bh-mongo');
var SectionClass = require('bh-section');

var mongo = new MongoClass();

mongo.connect()
  .then(function() {
    mongo.query(new SectionClass({
      "collection" : "test",
      "type" : "select",
      "search" : {
        "value" : {
          "$eq" : 1
      },
      "projection": {
        "value": 1
      },
      "sort": {
        "value": 1
      }
    }
    }))
    .then(function(result) {
      console.log(result);
      mongo.close();
    })
    .catch(function(error) {
      console.error(error);
      mongo.close();
    });
  })
  .catch(function(error) {
    console.error(error);
  });

```
