- Release Notes >
- Release Notes for MongoDB 2.4 >
- JavaScript Changes in MongoDB 2.4
JavaScript Changes in MongoDB 2.4¶
On this page
Consider the following impacts of V8 JavaScript Engine in MongoDB 2.4:
Tip
Use the new interpreterVersion()
method in the
mongo
shell and the javascriptEngine
field in the output of db.serverBuildInfo()
to determine
which JavaScript engine a MongoDB binary uses.
Improved Concurrency¶
Previously, MongoDB operations that required the JavaScript interpreter
had to acquire a lock, and a single mongod
could only run a
single JavaScript operation at a time. The switch to V8 improves
concurrency by permitting multiple JavaScript operations to run at the
same time.
Modernized JavaScript Implementation (ES5)¶
The 5th edition of ECMAscript, abbreviated as ES5, adds many new language features, including:
- standardized JSON,
- strict mode,
- function.bind(),
- array extensions, and
- getters and setters.
With V8, MongoDB supports the ES5 implementation of Javascript with the following exceptions.
Note
The following features do not work as expected on documents returned from MongoDB queries:
Object.seal()
throws an exception on documents returned from MongoDB queries.Object.freeze()
throws an exception on documents returned from MongoDB queries.Object.preventExtensions()
incorrectly allows the addition of new properties on documents returned from MongoDB queries.enumerable
properties, when added to documents returned from MongoDB queries, are not saved during write operations.
See SERVER-8216, SERVER-8223, SERVER-8215, and SERVER-8214 for more information.
For objects that have not been returned from MongoDB queries, the features work as expected.
Removed Non-Standard SpiderMonkey Features¶
V8 does not support the following non-standard SpiderMonkey JavaScript extensions, previously supported by MongoDB’s use of SpiderMonkey as its JavaScript engine.
E4X Extensions¶
V8 does not support the non-standard E4X extensions. E4X provides a native XML object to the JavaScript language and adds the syntax for embedding literal XML documents in JavaScript code.
You need to use alternative XML processing if you used any of the following constructors/methods:
XML()
Namespace()
QName()
XMLList()
isXMLName()
Destructuring Assignment¶
V8 does not support the non-standard destructuring assignments. Destructuring assignment “extract[s] data from arrays or objects using a syntax that mirrors the construction of array and object literals.” - Mozilla docs
Example
The following destructuring assignment is invalid with V8 and
throws a SyntaxError
:
Iterator()
, StopIteration()
, and Generators¶
V8 does not support Iterator(), StopIteration(), and generators.
InternalError()
¶
V8 does not support InternalError()
. Use Error()
instead.
for each...in
Construct¶
V8 does not support the use of for each…in
construct. Use for (var x in y)
construct
instead.
Example
The following for each (var x in y)
construct is invalid
with V8:
Instead, in version 2.4, you can use the for (var x in y)
construct:
You can also use the array instance method forEach()
with the
ES5 method Object.keys()
:
Array Comprehension¶
V8 does not support Array comprehensions.
Use other methods such as the Array
instance methods map()
,
filter()
, or forEach()
.
Example
With V8, the following array comprehension is invalid:
Instead, you can implement using the Array
instance method
forEach()
and the ES5 method Object.keys()
:
Note
The new logic uses the Array
instance method forEach()
and
not the generic method Array.forEach()
; V8 does not
support Array
generic methods. See Array Generic Methods for
more information.
Multiple Catch Blocks¶
V8 does not support multiple catch
blocks and will throw a
SyntaxError
.
Example
The following multiple catch blocks is invalid with V8 and will
throw "SyntaxError: Unexpected token if"
:
Conditional Function Definition¶
V8 will produce different outcomes than SpiderMonkey with conditional function definitions.
Example
The following conditional function definition produces different outcomes in SpiderMonkey versus V8:
With SpiderMonkey, the conditional function outputs undefined
,
whereas with V8, the conditional function outputs function
.
If your code defines functions this way, it is highly recommended that you refactor the code. The following example refactors the conditional function definition to work in both SpiderMonkey and V8.
The refactored code outputs undefined
in both SpiderMonkey and V8.
Note
ECMAscript prohibits conditional function definitions. To force V8
to throw an Error
, enable strict mode.
The JavaScript code throws the following syntax error:
String Generic Methods¶
V8 does not support String generics.
String generics are a set of methods on the String
class that
mirror instance methods.
Example
The following use of the generic method
String.toLowerCase()
is invalid with V8:
With V8, use the String
instance method toLowerCase()
available
through an instance of the String
class instead:
With V8, use the String
instance methods instead of following
generic methods:
String.charAt() |
String.quote() |
String.toLocaleLowerCase() |
String.charCodeAt() |
String.replace() |
String.toLocaleUpperCase() |
String.concat() |
String.search() |
String.toLowerCase() |
String.endsWith() |
String.slice() |
String.toUpperCase() |
String.indexOf() |
String.split() |
String.trim() |
String.lastIndexOf() |
String.startsWith() |
String.trimLeft() |
String.localeCompare() |
String.substr() |
String.trimRight() |
String.match() |
String.substring() |
Array Generic Methods¶
V8 does not support Array generic methods.
Array generics are a set of methods on the Array
class that mirror
instance methods.
Example
The following use of the generic method Array.every()
is
invalid with V8:
With V8, use the Array
instance method every()
available through
an instance of the Array
class instead:
With V8, use the Array
instance methods instead of the following
generic methods:
Array.concat() |
Array.lastIndexOf() |
Array.slice() |
Array.every() |
Array.map() |
Array.some() |
Array.filter() |
Array.pop() |
Array.sort() |
Array.forEach() |
Array.push() |
Array.splice() |
Array.indexOf() |
Array.reverse() |
Array.unshift() |
Array.join() |
Array.shift() |
Array Instance Method toSource()
¶
V8 does not support the Array
instance method toSource().
Use the Array
instance method toString()
instead.
uneval()
¶
V8 does not support the non-standard method uneval()
. Use the
standardized JSON.stringify()
method instead.