Leveraging ES6 in the Mongo shell

Having completed chapter 2, I remembered a remark that the Mongo shell uses JavaScript (isn’t it ECMAScript when there’s no browser DOM?), so I had a play to see what I can do with it. This is what I came up with. It’s a bit of a contrived example, but I think it demonstrates that there’s potential to do some seriously powerful stuff using the shell.

I’m connected to the Atlas cluster I created during chapter 2, which has a video database containing a movieDetails collection.

Here’s my foo.js file. I’m declaring a function called foo using ES6 (ECMAScript 2015) syntax, and also a class called Bar, which also uses ES6 syntax:

var foo = (databaseName) => {
  db = db.getSiblingDB(databaseName);
  return db.movieDetails.find().count();
}

class Bar {
  constructor(databaseName) {
    print(`Instantiating a Bar with databaseName ${databaseName}`);
    db = db.getSiblingDB(databaseName);
  }
  
  getMovieCount() {
    var count = db.movieDetails.find().count();
    print(`Bar.getMovieCount() found ${count} movies`);
    return count;
  }
  
  countMovies() {
    print(`The ${db} database contains ${this.getMovieCount()} movies`);
  }
}

In the Mongo shell, I execute the above file

MongoDB Enterprise Cluster0-shard-0:PRIMARY> load('foo.js')
true

It’s worth noting that running this file a second time in a single Mongo shell session results in an error because foo and Bar have already been defined. If I change the file and want to re-run it, I need to quit() out of the shell and re-connect to the cluster first.

I can see that I’ve declared things called foo and Bar:

MongoDB Enterprise Cluster0-shard-0:PRIMARY> typeof foo
function

MongoDB Enterprise Cluster0-shard-0:PRIMARY> foo
(databaseName) => {
  db = db.getSiblingDB(databaseName);
  return db.movieDetails.find().count();
}

MongoDB Enterprise Cluster0-shard-0:PRIMARY> typeof Bar
function

MongoDB Enterprise Cluster0-shard-0:PRIMARY> Bar
function Bar(databaseName) {
"use strict";

    print(`Instantiating a Bar with databaseName ${databaseName}`);
    db = db.getSiblingDB(databaseName);
  }

The output from the last command shows that my Bar class has been transpiled to an ES5 function (is there some Babel going on under the hood?) but that’s cool, that’s exactly what happens in a React application too, and it all still works.

Executing my foo function on the video database shows me that it contains 2295 documents:

MongoDB Enterprise Cluster0-shard-0:PRIMARY> foo('video')
2295

Executing it against a database which doesn’t exist:

MongoDB Enterprise Cluster0-shard-0:PRIMARY> foo('notADatabase')
0

Creating an instance of the Bar class:

MongoDB Enterprise Cluster0-shard-0:PRIMARY> var bar = new Bar('video')
Instantiating a Bar with databaseName video

Calling the countMovies method of the Bar class:

MongoDB Enterprise Cluster0-shard-0:PRIMARY> bar.countMovies()
Bar.getMovieCount() found 2295 movies
The video database contains 2295 movies

I know this is a bit off-topic as it’s not really what the M001 course is about, I imagine there’s another course all about it. But the geek in me got so excited about it that I had to share it :slight_smile:

2 Likes

Hi @Simon_39939,
I knew that mongo shell supports ES6. But never thought to do such experiments with it. Perhaps I can create some custom functions to manipulate crud process for better results or just for fun :stuck_out_tongue: .
Thanks for sharing.