When I'm running the following JavaScript script in MongoDb Shell (version 4.2), it says it doesn't understand the .set function?
const xxx = new Map();
print (xxx instanceof Map); // Output below displays "true"
xxx.set('bar', 'foo');
print('**************');
with the following error:
MongoDB shell version v4.2.18
connecting to: mongodb://mongodb.data:27017/?compressors=disabled&gssapiServiceName=mongodb
Implicit session: session { "id" : UUID("b1152dd2-02a2-4624-ae28-f3e4f34785b8") }
MongoDB server version: 4.2.18
true
2022-04-03T12:38:44.381 0000 E QUERY [js] uncaught exception: TypeError: xxx.set is not a function :
@run-update.js:5:1
2022-04-03T12:38:44.382 0000 F - [main] failed to load: run-update.js
2022-04-03T12:38:44.382 0000 E - [main] exiting with code -3
root@1548dcbfb906:/src/scripts#
Surely MongoDB shell can handle JavaScript Maps, right?
CodePudding user response:
The MongoDB shell implements its own version of the Map
type, rather than the "ES6" Map type. The MongoDB type uses put
instead of set
to add elements to the map, this is why MongoDB returns an error when trying to use set
.
The issue has been reported in the MongoDB issues tracker .