{"library":"mongo-clean","title":"MongoDB Database Cleaner","description":"mongo-clean is a focused utility designed to efficiently clear data from MongoDB databases, primarily used in testing or development environments. Version 2.0.0 is the current stable release. It offers two main strategies for cleaning: dropping entire collections or removing all documents within them, providing flexibility depending on schema requirements or performance considerations. A key feature is the ability to exclude specific collections from the cleaning process, preventing accidental data loss for essential tables. It supports both traditional callback-based asynchronous operations and modern Promise-based and `async/await` syntax, integrating seamlessly into contemporary Node.js applications. The package's minimalist API distinguishes it from more complex database management tools, focusing solely on the clean-up task. Its release cadence is not explicitly stated, but its clear purpose suggests a mature, low-churn library.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install mongo-clean"],"cli":null},"imports":["const clean = require('mongo-clean')","import clean from 'mongo-clean'"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const clean = require('mongo-clean');\nconst { MongoClient } = require('mongodb');\n\nconst url = \"mongodb://localhost:27017\";\nconst dbName = 'mongocleantest';\n\nasync function run() {\n  let client;\n  try {\n    // Connect to MongoDB using recommended options for modern driver\n    client = await MongoClient.connect(url, { useNewUrlParser: true, useUnifiedTopology: true });\n    const db = client.db(dbName);\n\n    // Optional: insert some dummy data to demonstrate cleaning\n    await db.collection('users').insertOne({ name: 'Alice' });\n    await db.collection('products').insertOne({ item: 'Laptop' });\n    console.log('Database populated with dummy data (users, products).');\n\n    // Clean the database by dropping collections, excluding 'products'\n    // This will remove the 'users' collection and its data, but 'products' will remain intact.\n    await clean(db, { exclude: ['products'] });\n    console.log('Database cleaned (dropping collections, excluding products).');\n\n    let usersCount = await db.collection('users').countDocuments();\n    let productsCount = await db.collection('products').countDocuments();\n    console.log(`Users collection count: ${usersCount}`); // Should be 0 (collection dropped)\n    console.log(`Products collection count: ${productsCount}`); // Should be 1 (original item still exists in retained collection)\n\n    // Re-populate a collection for the next step demonstration\n    await db.collection('temp_data').insertOne({ value: 123 });\n    console.log('Database re-populated with temporary data for \"remove\" action.');\n\n    // Clean the database by removing documents from all collections\n    // This will empty all collections including 'products' and 'temp_data' (unless explicitly excluded).\n    await clean(db, { action: 'remove' });\n    console.log('Database cleaned by removing documents from all collections.');\n\n    usersCount = await db.collection('users').countDocuments(); // 'users' collection might be recreated if accessed, but will be empty\n    productsCount = await db.collection('products').countDocuments();\n    const tempDataCount = await db.collection('temp_data').countDocuments();\n\n    console.log(`Users collection count after 'remove': ${usersCount}`); // Should be 0\n    console.log(`Products collection count after 'remove': ${productsCount}`); // Should be 0\n    console.log(`Temp Data collection count after 'remove': ${tempDataCount}`); // Should be 0\n\n  } catch (err) {\n    console.error('An error occurred:', err);\n  } finally {\n    if (client) {\n      await client.close();\n      console.log('MongoDB connection closed.');\n    }\n  }\n}\n\nrun();","lang":"javascript","description":"Demonstrates connecting to MongoDB, populating dummy data, cleaning the database by dropping collections (with exclusions), and then cleaning by removing documents from all collections, illustrating both primary cleaning methods and proper client closure.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}