Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mcollina/mongo-clean

Clean all the collections in a mongo database
https://github.com/mcollina/mongo-clean

Last synced: about 1 month ago
JSON representation

Clean all the collections in a mongo database

Awesome Lists containing this project

README

        

mongo-clean
===========

[![Build
Status](https://travis-ci.org/mcollina/mongo-clean.svg)](https://travis-ci.org/mcollina/mongo-clean)

Clean all the collections in a mongo database

## Install

```bash
npm install mongo-clean --save-dev
```

## Usage

Pass the database handler to `clean`.

```js
var clean = require('mongo-clean')
var MongoClient = require('mongodb').MongoClient
var url = "mongodb://localhost:27017"

MongoClient.connect(url, { w: 1 }, function (err, client) {
clean(client.db('mongocleantest'), function (err) {
// your db is clean!
})
})
```

Clean the db excluding a list of collections

```js
var clean = require('mongo-clean')
var MongoClient = require('mongodb').MongoClient
var url = "mongodb://localhost:27017"

MongoClient.connect(url, { w: 1 }, function (err, client) {
clean(client.db('mongocleantest'), {exclude: ['dummy1', 'dummy2']}, function (err) {
// Delete all the collections in the db except dummy1 and dummy2
})
})
```

Removing all elements instead of dropping the collections:

```js
var clean = require('mongo-clean')
var MongoClient = require('mongodb').MongoClient
var url = "mongodb://localhost:27017"

MongoClient.connect(url, { w: 1 }, function (err, client) {
clean(client.db('mongocleantest'), { action: 'remove' }, function (err) {
// automatically removes all the data from all the collections in the db
})
})
```

Promises and *async await* are supported as well.
```js
var clean = require('mongo-clean')
var MongoClient = require('mongodb').MongoClient
var url = "mongodb://localhost:27017"

MongoClient.connect(url, { w: 1 }, function (err, client) {
clean(client.db('mongocleantest'))
.then(console.log)
.catch(console.log)
})
```

## License

MIT