Skip to content

mcollina/mongo-clean

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Clean all the collections in a mongo database

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

 

Packages

No packages published

Contributors 3

  •  
  •  
  •