Robomongo ECMAScript 2015 (aka ES6) Support

Gökhan Simsek edited this page Jun 13, 2017 · 2 revisions

In MongoDB 3.2, the javascript engine used for both the mongo shell and for server-side javascript in mongod changed from V8 to SpiderMonkey. SpiderMonkey brought increased support for features defined in officially known as ECMAScript 2015 (aka 6th edition of ECMAScript, abbreviated as ES6).

Within Robomongo 1.1 release, we have also added support for ECMAScript 2015 in Robomongo shell.

Let's see some of the ECMAScript 2015 features with examples:

i. Arrow Functions

Arrow functions are a great addition to the JavaScript language. They make for short and concise code.

// With ECMAScript 2015: Arrow Functions
db.Bills.find({}).limit(3).map( doc => doc._id );

// Before ECMAScript 2015
db.Bills.find({}).limit(3).map( function(doc) { 
    return doc._id; 

// Output:

ii. Computed Property Names

Before ECMAScript 2015, computed property names such as [month + '20'] was causing the whole script to fail and give error. In this release they are usable.

iii. Default Values for Function Parameters

Here is an example of default values for function parameters how it was done before ECMAScript 2015 and how easy now with ECMAScript 2015:

// ECMAScript 2015
var link = function(height = 50, color = 'red', url = '') {
    // ... some code

// Before ECMAScript 2015
var link = function (height, color, url) 
    var height = height || 50
    var color = color || 'red'
    var url = url || ''
    // ... some code

iv. Two new ways of declaring variables: 'let' and 'const'

ECMAScript 2015 provides two new ways of declaring variables: let and const.

a. let:
let works similarly to var, but the variable it declares is block-scoped, it only exists within the current block. var is function-scoped.

In the following code, the let-declared variable tmp only exists inside the block that starts in line (A):

function order(x, y) {
    if (x > y) { // (A)
        let tmp = x;
        x = y;
        y = tmp;
    console.log(tmp===x); // ReferenceError: tmp is not defined
    return [x, y];

b. const:
const works like let, but the variable you declare must be immediately initialized, with a value that can’t be changed afterwards.

    // SyntaxError: Missing initializer in const declaration

const MAX_COLLECTION_SIZE = 2000; // Ok

// We are protected to unintentional changes
    // TypeError: `MAX_COLLECTION_SIZE` is read-only


JavaScript Changes in MongoDB 3.2:

JavaScript Reference

Exploring ES6 book by Dr Axel Rauschmayer - 2015

ECMAScript® 2015 Language Specification:

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.