dcg

MongoDB

database.mongodb

Protects against destructive MongoDB operations like dropDatabase, dropCollection, and remove without criteria

  • 7blocking rules
  • 5safe patterns
  • 1Critical
  • 6High
~/.config/dcg/config.toml
[packs]
enabled = ["database.mongodb"]

Evaluated when a command contains mongo, mongosh, dropDatabase, dropCollection, deleteMany, mongorestore, mongodump.

Recorded verdicts

  • mongosh --eval "db.dropDatabase()"Blockeddatabase.mongodb:drop-database

Blocking rules

Each rule ID is database.mongodb:<name>. Use it in allowlists and [policy.rules].

  1. drop-database

    Critical

    dropDatabase permanently deletes the entire database.

    Explanation
    dropDatabase() removes the current database completely: - All collections are dropped - All indexes are removed - All documents are gone - Users scoped to the database lose access This cannot be undone. The database can be recreated but data is lost. Backup first: mongodump --db=dbname --out=/path/to/backup List databases: show dbs
  2. collection-drop

    High

    collection.drop() permanently deletes the collection.

    Explanation
    db.<collection>.drop() is the most common way to delete a collection: - All documents in the collection are deleted - Indexes on the collection are removed - Cannot be undone Before dropping: db.collection.stats() // Size and document count db.collection.find().limit(5) // Sample documents Backup: mongodump --db=mydb --collection=mycollection
  3. delete-all

    High

    remove({}) or deleteMany({}) deletes ALL documents. Add filter criteria.

    Explanation
    An empty filter {} matches ALL documents in the collection: - deleteMany({}) removes every document - remove({}) (deprecated) does the same - Collection structure and indexes remain If you want to delete everything, drop() is faster. Otherwise, add filter criteria: db.collection.deleteMany({ status: 'expired' }) Preview what would be deleted: db.collection.countDocuments({}) // All documents! db.collection.find({}).limit(10) // Sample docs
  4. drop-collection

    High

    drop/dropCollection permanently deletes the collection.

    Explanation
    drop() removes the entire collection: - All documents are deleted - All indexes are removed - Schema validation rules are lost - Cannot be undone Backup collection first: mongodump --db=dbname --collection=collname Count documents before dropping: db.collection.countDocuments()
  5. mongorestore-drop

    High

    mongorestore --drop deletes existing data before restoring.

    Explanation
    mongorestore --drop drops collections before restoring them: - Existing data is deleted first - If restore fails partway, data may be lost - Only affects collections being restored Safer approaches: - Restore to a different database first - Use --nsExclude to skip certain collections - Remove --drop and handle conflicts manually Test restore: mongorestore --db=test_restore --drop /backup/path
  6. stdin-unverified

    High

    mongosh receives indirect input that dcg cannot statically verify.

    Explanation
    Materialize and review the exact JavaScript before piping or redirecting it into mongosh.
  7. update-all

    High

    updateMany({}, ...) overwrites fields in ALL documents. Add filter criteria.

    Explanation
    An empty filter {} matches every document, so the update rewrites the whole collection and the previous values are gone. Add filter criteria: db.collection.updateMany({ status: 'pending' }, { $set: { ... } }) Preview what would change: db.collection.countDocuments({}) // All documents!

Safe patterns

Checked first. A command that matches one is never blocked by this pack.

  • mongo-find
  • mongo-count
  • mongo-aggregate
  • mongodump-no-drop
  • mongo-explain