# asyncmongodb

> Easiest module to connect to mongodb by promise or async/await

Latest version **1.4.1** (published 2018-03-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install asyncmongodb
pnpm add asyncmongodb
yarn add asyncmongodb
bun add asyncmongodb
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.4.1 |
| Published | 2018-03-23 |
| First published | 2018-03-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 22.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Abdelrahman Mohamed |
| Maintainers | mrabdelrahman10 |
| Keywords | mongo, mongodb, async |

## Links

- npm: https://www.npmjs.com/package/asyncmongodb
- Repository: https://github.com/MrAbdelrahman10/asyncmongodb
- Homepage: https://github.com/MrAbdelrahman10/asyncmongodb#readme
- Issues: https://github.com/MrAbdelrahman10/asyncmongodb/issues
- npm.io page: https://npm.io/package/asyncmongodb

## Dependencies (1)

- [mongodb](https://npm.io/package/mongodb.md) ^3.0.4

## Alternatives

- [angular-pipes](https://npm.io/package/angular-pipes.md) — 5.6K weekly downloads
- [@ng-web-apis/midi](https://npm.io/package/@ng-web-apis/midi.md) — 2.6K weekly downloads
- [happn-3](https://npm.io/package/happn-3.md) — 1.6K weekly downloads
- [@opensip-cli/lang-go](https://npm.io/package/@opensip-cli/lang-go.md) — 1.2K weekly downloads
- [mongoose-typescript](https://npm.io/package/mongoose-typescript.md) — 85 weekly downloads

## Recent versions

- 1.4.1 (latest) — 2018-03-23
- 1.4.0 — 2018-03-22
- 1.3.0 — 2018-03-19
- 1.2.0 — 2018-03-18
- 1.0.0 — 2018-03-17
- 0.0.2 — 2018-03-15
- 0.0.1 — 2018-03-15

## README

# Async Mongodb

## Description

Easiest module to connect to mongodb by promise or async/await

## Documentation

| what          | where                                          |
|---------------|------------------------------------------------|
| documentation | https://github.com/MrAbdelrahman10/asyncmongodb  |
| source        | https://github.com/MrAbdelrahman10/asyncmongodb |

### Bugs / Feature Requests

Think you’ve found a bug? Want to see a new feature in `asyncmongodb`? Please open an Issue here [https://github.com/MrAbdelrahman10/asyncmongodb/issues](https://github.com/MrAbdelrahman10/asyncmongodb/issues)

### Change Log

Change history can be found in [`HISTORY.md`](HISTORY.md).

# Installation

The recommended way to get started using the `Async Mongodb` is by using the `npm` (Node Package Manager) to install the dependency in your project.

Given that you have created your own project using `npm init` we install `Async Mongodb` and its dependencies by executing the following `npm` command.

```bash
npm install asyncmongodb --save
```

This will download the `asyncmongodb` and add a dependency entry in your `package.json` file.

## Quick Start

This guide will show you how to set up a simple application using `asyncmongodb`. 

### Create the `package.json` file

First, create a directory where your application will live.

```bash
mkdir myproject
cd myproject
```

Enter the following command and answer the questions to create the initial structure for your new project:

```bash
npm init
```

Next, install `asyncmongodb` dependency.

```bash
npm install asyncmongodb --save
```

You should see **NPM** download a lot of files. Once it's done you'll find all the downloaded packages under the **node_modules** directory.


### Connect to MongoDB

Create a new **app.js** file and add the following code to try out some basic CRUD
operations using the **AsyncMongoDB**.

Add code to connect to the server and the database **myproject**:

Using **Async/Await**
```js
// Load Module
const Asyncmongodb = require('asyncmongodb');
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';

// Run test
(async () => {
    const amng = new Asyncmongodb({ dbName: dbName, uri: url });
    await amng.connect();
    /**
	 Some Code
	*/
    amng.disconnect();
})();
```

Using **Promise**
```js
// Load Module
const Asyncmongodb = require('asyncmongodb');
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';

// Run test
const amng = new Asyncmongodb({ dbName: dbName, uri: url });
amng.connect()
    .then(/** Some Code **/)
    .then(_ => amng.disconnect())
    .catch((e) => {
        /** Some Code **/
        amng.disconnect()
    });
```

### Find Documents

Use **find()** to return all documents:

Using **Async/Await**
```js
// Load Module
const Asyncmongodb = require('asyncmongodb');
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';

// Run test
(async () => {
    const amng = new Asyncmongodb({ dbName: dbName, uri: url });
    await amng.connect();
    const data = await mng.find({collection: 'countries', where:{}, fields:{}, sort: {name: 1}, limit: 10});
    /**
	 Some Code
	*/
    amng.disconnect();
})();
```

Using **Promise**
```js
// Load Module
const Asyncmongodb = require('asyncmongodb');
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';

// Run test
const amng = new Asyncmongodb({ dbName: dbName, uri: url });
amng.connect()
    .then(_ => mng.find({ collection: 'countries', where: {}, fields: {}, sort: { name: 1 }, skip: 1, limit: 10 }))
    .then(/** Some Code **/)
    .then(_ => amng.disconnect())
    .catch((e) => {
        /** Some Code **/
        amng.disconnect()
    });
```


### Find One Document

Use **findOne()** to return one document:

Using **Async/Await**
```js
// Load Module
const Asyncmongodb = require('asyncmongodb');
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';

// Run test
(async () => {
    const amng = new Asyncmongodb({ dbName: dbName, uri: url });
    await amng.connect();
    const data = await mng.findOne({collection: 'countries', where:{}, sort: { name: 1 }, skip: 1, fields:{}, limit: 10});
    /**
	 Some Code
	*/
    amng.disconnect();
})();
```

Using **Promise**
```js
// Load Module
const Asyncmongodb = require('asyncmongodb');
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';

// Run test
const amng = new Asyncmongodb({ dbName: dbName, uri: url });
amng.connect()
    .then(_ => mng.findOne({ collection: 'countries', where: {}, fields: {}, limit: 10 }))
    .then(/** Some Code **/)
    .then(_ => amng.disconnect())
    .catch((e) => {
        /** Some Code **/
        amng.disconnect()
    });
```

### Insert One Document

Use **insert()** to insert one document:

Using **Async/Await**
```js
// Load Module
const Asyncmongodb = require('asyncmongodb');
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';

// Run test
(async () => {
    const amng = new Asyncmongodb({ dbName: dbName, uri: url });
    await amng.connect();
    const data = await mng.insert({collection: 'countries', row:{name: "Egypt"}});
    /**
	 Some Code
	*/
    amng.disconnect();
})();
```

Using **Promise**
```js
// Load Module
const Asyncmongodb = require('asyncmongodb');
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';

// Run test
const amng = new Asyncmongodb({ dbName: dbName, uri: url });
amng.connect()
    .then(_ => mng.insert({ collection: 'countries', row: { name: "Egypt" } }))
    .then(/** Some Code **/)
    .then(_ => amng.disconnect())
    .catch((e) => {
        /** Some Code **/
        amng.disconnect()
    });
```


### Insert Many Documents

Use **insertMany()** to insert many documents:

Using **Async/Await**
```js
// Load Module
const Asyncmongodb = require('asyncmongodb');
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';

// Run test
(async () => {
    const amng = new Asyncmongodb({ dbName: dbName, uri: url });
    await amng.connect();
    const res = await mng.insertMany({ collection: 'countries', rows: [{ _id: 1, name: 'egypt' }, { _id: 2, name: 'turkey' }, { _id: 3, name: 'malaysia' }], ignoreErrors: true }).catch((e) => e.code);
    /**
	 Some Code
	*/
    amng.disconnect();
})();
```

Using **Promise**
```js
// Load Module
const Asyncmongodb = require('asyncmongodb');
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';

// Run test
const amng = new Asyncmongodb({ dbName: dbName, uri: url });
amng.connect()
    .then(_ => mng.insertMany({ collection: 'countries', rows: [{ _id: 1, name: 'egypt' }, { _id: 2, name: 'turkey' }, { _id: 3, name: 'malaysia' }], ignoreErrors: true }).catch((e) => e.code))
    .then(/** Some Code **/)
    .then(_ => amng.disconnect())
    .catch((e) => {
        /** Some Code **/
        amng.disconnect()
    });
```

### Update One Document

Use **update()** to update one document:

Using **Async/Await**
```js
// Load Module
const Asyncmongodb = require('asyncmongodb');
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';

// Run test
(async () => {
    const amng = new Asyncmongodb({ dbName: dbName, uri: url });
    await amng.connect();
    const data = await mng.update({collection: 'countries', where: {_id: 1}, row:{name: "Egypt"}});
    /**
	 Some Code
	*/
    amng.disconnect();
})();
```

Using **Promise**
```js
// Load Module
const Asyncmongodb = require('asyncmongodb');
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';

// Run test
const amng = new Asyncmongodb({ dbName: dbName, uri: url });
amng.connect()
    .then(_ => mng.update({ collection: 'countries', where: { _id: 1 }, row: { name: "Egypt" } }))
    .then(/** Some Code **/)
    .then(_ => amng.disconnect())
    .catch((e) => {
        /** Some Code **/
        amng.disconnect()
    });
```


### Update Many Documents

Use **updateMany()** to update many documents:

Using **Async/Await**
```js
// Load Module
const Asyncmongodb = require('asyncmongodb');
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';

// Run test
(async () => {
    const amng = new Asyncmongodb({ dbName: dbName, uri: url });
    await amng.connect();
    const res = await mng.updateMany({ collection: 'countries', rows: [{ _id: 1, name: 'egypt' }, { _id: 2, name: 'turkey' }, { _id: 3, name: 'malaysia' }], ignoreErrors: true }).catch((e) => e.code);
    /**
	 Some Code
	*/
    amng.disconnect();
})();
```

Using **Promise**
```js
// Load Module
const Asyncmongodb = require('asyncmongodb');
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';

// Run test
const amng = new Asyncmongodb({ dbName: dbName, uri: url });
amng.connect()
    .then(_ => mng.updateMany({ collection: 'countries', rows: [{ _id: 1, name: 'egypt' }, { _id: 2, name: 'turkey' }, { _id: 3, name: 'malaysia' }], ignoreErrors: true }).catch((e) => e.code))
    //.then(/** Some Code **/)
    .then(_ => amng.disconnect())
    .catch((e) => {
        /** Some Code **/
        amng.disconnect()
    });
```

### Delete One Document

Use **delete()** to delete one document:

Using **Async/Await**
```js
// Load Module
const Asyncmongodb = require('asyncmongodb');
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';

// Run test
(async () => {
    const amng = new Asyncmongodb({ dbName: dbName, uri: url });
    await amng.connect();
    const data = await mng.delete({collection: 'countries', where: {_id: 1}});
    /**
	 Some Code
	*/
    amng.disconnect();
})();
```

Using **Promise**
```js
// Load Module
const Asyncmongodb = require('asyncmongodb');
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';

// Run test
const amng = new Asyncmongodb({ dbName: dbName, uri: url });
amng.connect()
    .then(_ => mng.delete({ collection: 'countries', where: { _id: 1 } }))
    .then(/** Some Code **/)
    .then(_ => amng.disconnect())
    .catch((e) => {
        /** Some Code **/
        amng.disconnect()
    });
```


### Delete Many Documents

Use **deleteMany()** to delete many documents:

Using **Async/Await**
```js
// Load Module
const Asyncmongodb = require('asyncmongodb');
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';

// Run test
(async () => {
    const amng = new Asyncmongodb({ dbName: dbName, uri: url });
    await amng.connect();
    const res = await mng.deleteMany({ collection: 'countries', where: {}, ignoreErrors: true }).catch((e) => e.code);
    /**
	 Some Code
	*/
    amng.disconnect();
})();
```

Using **Promise**
```js
// Load Module
const Asyncmongodb = require('asyncmongodb');
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';

// Run test
const amng = new Asyncmongodb({ dbName: dbName, uri: url });
amng.connect()
    .then(_ => mng.deleteMany({ collection: 'countries', where: {}, ignoreErrors: true }).catch((e) => e.code))
    .then(/** Some Code **/)
    .then(_ => amng.disconnect())
    .catch((e) => {
        /** Some Code **/
        amng.disconnect()
    });
```

### Count Documents

Use **count()** to get count of documents:

Using **Async/Await**
```js
// Load Module
const Asyncmongodb = require('asyncmongodb');
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';

// Run test
(async () => {
    const amng = new Asyncmongodb({ dbName: dbName, uri: url });
    await amng.connect();
    const res = await mng.count({ collection: 'countries', where: {} }).catch((e) => e.code);
    /**
	 Some Code
	*/
    amng.disconnect();
})();
```

Using **Promise**
```js
// Load Module
const Asyncmongodb = require('asyncmongodb');
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';

// Run test
const amng = new Asyncmongodb({ dbName: dbName, uri: url });
amng.connect()
    .then(_ => mng.count({ collection: 'countries', where: {}, ignoreErrors: true }).catch((e) => e.code))
    .then(/** Some Code **/)
    .then(_ => amng.disconnect())
    .catch((e) => {
        /** Some Code **/
        amng.disconnect()
    });
```

### Drop Collection

Use **dropCollection()** to drop collections:

Using **Async/Await**
```js
// Load Module
const Asyncmongodb = require('asyncmongodb');
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';

// Run test
(async () => {
    const amng = new Asyncmongodb({ dbName: dbName, uri: url });
    await amng.connect();
    const res = await mng.dropCollection('countries').catch((e) => e.code);
    /**
	 Some Code
	*/
    amng.disconnect();
})();
```

Using **Promise**
```js
// Load Module
const Asyncmongodb = require('asyncmongodb');
// Connection URL
const url = 'mongodb://localhost:27017';
// Database Name
const dbName = 'myproject';

// Run test
const amng = new Asyncmongodb({ dbName: dbName, uri: url });
amng.connect()
    .then(_ => mng.dropCollection('countries').catch((e) => e.code))
    .then(/** Some Code **/)
    .then(_ => amng.disconnect())
    .catch((e) => {
        /** Some Code **/
        amng.disconnect()
    });
```

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