Skip to content

Latest commit

 

History

History
74 lines (56 loc) · 1.7 KB

README.md

File metadata and controls

74 lines (56 loc) · 1.7 KB

mongo-clean

Build Status

Clean all the collections in a mongo database

Install

npm install mongo-clean --save-dev

Usage

Pass the database handler to clean.

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

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:

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.

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