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:
[
    ObjectId("59228b0574d828afae4876cd"),
    ObjectId("59228b0574d828afae4876ce"),
    ObjectId("59228b0574d828afae4876cf")
]

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 = 'blog.robomongo.org') {
    // ... some code
}

// Before ECMAScript 2015
var link = function (height, color, url) 
{
    var height = height || 50
    var color = color || 'red'
    var url = url || 'blog.robomongo.org'
    // ... 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.

const MAX_COLLECTION_SIZE;
    // SyntaxError: Missing initializer in const declaration

const MAX_COLLECTION_SIZE = 2000; // Ok

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

References:

JavaScript Changes in MongoDB 3.2:
https://docs.mongodb.com/manual/release-notes/3.2-javascript/

JavaScript Reference
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference

Exploring ES6 book by Dr Axel Rauschmayer - 2015
http://exploringjs.com/es6/index.html

ECMAScript® 2015 Language Specification:
http://www.ecma-international.org/ecma-262/6.0/index.html

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.