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?

Cody
  • 123
  • 6

1 Answers1

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.

jQueeny
  • 156
  • 3