Skip to content

Commit

Permalink
Merge pull request #9938 from HunterKohler/id-map
Browse files Browse the repository at this point in the history
Enhancement: Cast ObjectId to string inside of MongooseMap.
  • Loading branch information
vkarpov15 authored Feb 15, 2021
2 parents aef25e0 + 8d0311e commit 791fe72
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/types/map.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const Mixed = require('../schema/mixed');
const ObjectId = require('./objectid');
const deepEqual = require('../utils').deepEqual;
const get = require('../helpers/get');
const handleSpreadDoc = require('../helpers/document/handleSpreadDoc');
Expand Down Expand Up @@ -41,6 +42,10 @@ class MongooseMap extends Map {
}

get(key, options) {
if (key instanceof ObjectId) {
key = key.toString();
}

options = options || {};
if (options.getters === false) {
return super.get(key);
Expand All @@ -49,6 +54,10 @@ class MongooseMap extends Map {
}

set(key, value) {
if (key instanceof ObjectId) {
key = key.toString();
}

checkValidKey(key);
value = handleSpreadDoc(value);

Expand Down Expand Up @@ -107,6 +116,10 @@ class MongooseMap extends Map {
}

delete(key) {
if (key instanceof ObjectId) {
key = key.toString();
}

this.set(key, undefined);
super.delete(key);
}
Expand Down

0 comments on commit 791fe72

Please sign in to comment.