Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/mcollina/mongo-clean
- Owner: mcollina
- License: mit
- Created: 2014-04-23T17:21:36.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2018-03-25T18:43:44.000Z (over 6 years ago)
- Last Synced: 2024-11-01T12:42:22.012Z (about 2 months ago)
- Language: JavaScript
- Size: 21.5 KB
- Stars: 12
- Watchers: 4
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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