# mongo-wrapper

> tiny wrapper around the native mongodb module

Latest version **0.0.29** (published 2015-02-17) · BSD license · 0 weekly downloads

## Install

```sh
npm install mongo-wrapper
pnpm add mongo-wrapper
yarn add mongo-wrapper
bun add mongo-wrapper
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.29 |
| Published | 2015-02-17 |
| First published | 2013-03-04 |
| Weekly downloads | 0 |
| License | BSD |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 4 |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 8 |
| Author | Dan McAulay & Jesse Ditson |
| Maintainers | dmcaulay |

## Links

- npm: https://www.npmjs.com/package/mongo-wrapper
- Repository: https://github.com/dmcaulay/mongo-wrapper
- Issues: https://github.com/dmcaulay/mongo-wrapper/issues
- npm.io page: https://npm.io/package/mongo-wrapper

## Dependencies (4)

- [async](https://npm.io/package/async.md) ^0.9.0
- [mongodb](https://npm.io/package/mongodb.md) ^1.4.31
- [underscore](https://npm.io/package/underscore.md) ^1.7.0
- [async-resource](https://npm.io/package/async-resource.md) 0.0.7

## Recent versions

- 0.0.29 (latest) — 2015-02-17
- 0.0.28 — 2014-07-27
- 0.0.27 — 2014-07-16
- 0.0.26 — 2014-06-25
- 0.0.25 — 2014-05-06
- 0.0.24 — 2014-03-19
- 0.0.23 — 2014-02-25
- 0.0.22 — 2014-02-24
- 0.0.21 — 2014-02-24
- 0.0.11 — 2013-12-23
- 0.0.10 — 2013-12-15
- 0.0.9 — 2013-12-15
- 0.0.8 — 2013-12-01
- 0.0.7 — 2013-11-21
- 0.0.6 — 2013-11-21
- … 6 more at https://npm.io/package/mongo-wrapper/versions

## README

# mongo-wrapper

This is a lightweight wrapper around the mongodb driver for Node.js. It simplifies connecting and adds a few helper methods.

## Installation

```bash
$ npm install mongo-wrapper
```

## Usage

### Configuration

```js
var mongo = require('mongo-wrapper');

var config = {
  username: 'admin_user',
  password: 'secret',
  hosts: [ 
    {name: 'primary-1.db.com', port: 27017}, 
    {name: 'secondary-1.db.com', port: 27017},
    {name: 'secondary-2.db.com', port: 27017} 
  ],
  database: 'db_1',
  options: {
    replicaSet: 'replicaset_1',
    readPreference: 'primaryPreferred'
  },
  indexes: {
    users: [
      {index: 'email', options: {unique: true}}
    ]
  }
};

db = mongo.setup(config);

// add your collections
db.add('users');
```

### Query

```js
// once the db is setup and collections are added you can use any
// methods found in the Mongo Driver Collection class
// http://mongodb.github.io/node-mongodb-native/api-generated/collection.html

db.users.insert({email: 'dan@email.com'}, function(err) {
  // inserted
});

db.users.findOne({email: 'dan@email.com'}, function(err, user) {
  // use user
});
```

### Additional Helper Functions

#### findArray()

```js
// findArray() simply calls toArray() on the cursor returned from
// Collection.find()
db.users.findArray(function(err, users) {
  // use users
});
```

#### findById(), updateById(), findAndModifyById()

```js
// These functions make it simple to query by ObjectId and they
// accept either and ObjectId object or a JavaScript String.
db.users.findById('53683ca19c49151345e479ad', function(err, user) {
  // use user
});
```

### bind()

```js
// This allows you to bind a function on a collection so you can
// easily use mongo-wrapper with libraries like async
var user_id = db.id('53683ca19c49151345e479ad');
async.parallel({
  user: db.users.bind('findById', user_id),
  reservations: db.reservations.bind('findArray', {user_id: user_id})
}, callback);
```

#### db.id()

```js
// a wrapper around ObjectId
db.id(); // creates a new ObjectId
db.id('53683ca19c49151345e479ad'); // creates the ObjectId
db.id('invalid id'); // returns null
```

---
_Source: https://npm.io/package/mongo-wrapper · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
