mongo-clean

2.0.0 • Public • Published

mongo-clean

BuildStatus

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

/mongo-clean/

    Package Sidebar

    Install

    npm i mongo-clean

    Weekly Downloads

    677

    Version

    2.0.0

    License

    MIT

    Unpacked Size

    9.67 kB

    Total Files

    6

    Last publish

    Collaborators

    • matteo.collina