1

Reading Mongo: Definite guide:

In first chapter, author mentions the following snippet to change some of the default commands of db:

var no = function() {
   print("Not on my watch.");
};

// Prevent dropping databases
db.dropDatabase = DB.prototype.dropDatabase = no;

// Prevent dropping collections
DBCollection.prototype.drop = no;

// Prevent dropping an index
DBCollection.prototype.dropIndex = no;

// Prevent dropping indexes
DBCollection.prototype.dropIndexes = no;

I know db is pointing to current database, but my question is from where does DB is coming from?

1
  • 1
    If you want to prevent execution of dangerous commands, then you should set roles and privileges accordingly. Overwriting the methods in your (private) shell does not add any security. Apr 2 at 8:58

1 Answer 1

4

That is referring to the legacy mongo shell which was deprecated in MongoDB v5.0 and has been replaced by mongosh. There is no DB in the mongosh shell.

If you are using an older version of MongoDB and have the mongo shell installed then DB is a global variable which resolves to a function with properties set by the Immediately Invoked Function Expression (IIFE) as part of the db.js execution.

The DB.prototype.dropDatabase is a function too so by running db.dropDatabase = DB.prototype.dropDatabase = no; you are referring to the dropDatabase function which is now pointing to your no function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.