I am trying to use the MongoDB shell to run some scripts. Inside the JavaScript I am attempting to run var map = new Map() then map.set(1,1).

However, I am getting the following error:

E QUERY    [js] TypeError: map.set is not a function :

I am running this mongo shell version: MongoDB shell version v4.2.1

Does the mongo shell not support the map.set() function?

2 Answers 2


No, they hide the standard implementation in implscope.cpp

// This builtin map is a javascript 6 thing. We want our version. so // take theirs out

and implement a new one in types.js.

Well, you can workaround doing:

Map.prototype.set = Map.prototype.put

The MongoDB shell currently implements its own custom Map type, rather than the ECMAScript 6 Map type. Amongst the differences, the MongoDB type uses put instead of set to add elements to the map. This explains why you're seeing the "map.set is not a function" error.

The three API functions on this custom map type are "get", "put", and "values".

> new Map();
    "_data" : {
    "put" : function(key, value) {
    var o = this._get(key);
    var old = o.value;
    o.value = value;
    return old;
    "get" : function(key) {
    return this._get(key).value;
    "_get" : function(key) {
    var h = Map.hash(key);
    var a = this._data[h];
    if (!a) {
        a = [];
        this._data[h] = a;
    for (var i = 0; i < a.length; i++) {
        if (friendlyEqual(key, a[i].key)) {
            return a[i];
    var o = {key: key, value: null};
    return o;
    "values" : function() {
    var all = [];
    for (var k in this._data) {
        this._data[k].forEach(function(z) {
    return all;

MongoDB issue SERVER-46243 was created in February 2020, and proposes to use the standard ECMAScript Map type instead. Once this change is implemented, your current code will work.

In summary, your example code should work as is if you change set to put:

var map = new Map();
map.put(1, 1);

Note that this is no longer an issue in the new mongosh MongoDB Shell (currently in Beta), as it uses the standard ECMAScript 6 Map type. This was mention in an April 2021 comment on the aforementioned ticket:

this feature is available in our new MongoDB Shell (mongosh) [...]. mongosh is still in beta but it should have most of the functionality you need. [...] we are preparing to GA it later this year [...].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.