Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't save values for particular keys of Map #15125

Open
2 tasks done
holem opened this issue Dec 22, 2024 · 1 comment
Open
2 tasks done

Can't save values for particular keys of Map #15125

holem opened this issue Dec 22, 2024 · 1 comment
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.

Comments

@holem
Copy link

holem commented Dec 22, 2024

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the bug has not already been reported

Mongoose version

8.9.0

Node.js version

22.9

MongoDB server version

8.0.1

Typescript version (if applicable)

No response

Description

For the field of Map type - if key in format "[number]" (number in square brackets) or "[number" (number preceded by square bracket) then it's not saving.

Steps to Reproduce

const ThingSchema = new mongoose.Schema({
  nums: { type: Map, of: [{ type: Number }], default: new Map() },
});
const Thing = mongoose.model("Thing", ThingSchema);

const thing = new Thing();

thing.nums.set("[0]", [1]);
thing.nums.set("[111]", [2]);
thing.nums.set("[,]", [3]);
thing.nums.set("[!]", [4]);
thing.nums.set("[111-]", [5]);
thing.nums.set("[-111]", [6]);
thing.nums.set("111]", [7]);
thing.nums.set("[111", [8]);
thing.nums.set("111", [9]);
thing.nums.set("0", [10]);

console.log("thing.nums", user.nums);
await thing.save();

const thing2 = await Thing.findById(thing._id.toString());
console.log("thing2.nums", thing2.nums);

output:

thing.nums Map(10) {
  '[0]' => [ 1 ],
  '[111]' => [ 2 ],
  '[,]' => [ 3 ],
  '[!]' => [ 4 ],
  '[111-]' => [ 5 ],
  '[-111]' => [ 6 ],
  '111]' => [ 7 ],
  '[111' => [ 8 ],
  '111' => [ 9 ],
  '0' => [ 10 ]
}
thing2.nums Map(7) {
  '0' => [ 10 ],
  '111' => [ 9 ],
  '111]' => [ 7 ],
  '[!]' => [ 4 ],
  '[,]' => [ 3 ],
  '[-111]' => [ 6 ],
  '[111-]' => [ 5 ]
}

Expected Behavior

All values ​​for all string keys are preserved.

@vkarpov15 vkarpov15 added this to the 8.9.4 milestone Dec 31, 2024
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Dec 31, 2024
@vkarpov15
Copy link
Collaborator

I'm unable to repro. Given the following script:

'use strict';

import mongoose from 'mongoose';

console.log('Mongoose version', mongoose.version);

const ThingSchema = new mongoose.Schema({
  nums: { type: Map, of: [{ type: Number }], default: new Map() },
});
const Thing = mongoose.model("Thing", ThingSchema);

const thing = new Thing();

thing.nums.set("[0]", [1]);
thing.nums.set("[111]", [2]);
thing.nums.set("[,]", [3]);
thing.nums.set("[!]", [4]);
thing.nums.set("[111-]", [5]);
thing.nums.set("[-111]", [6]);
thing.nums.set("111]", [7]);
thing.nums.set("[111", [8]);
thing.nums.set("111", [9]);
thing.nums.set("0", [10]);

await mongoose.connect('mongodb://127.0.0.1:27017/mongoose_test');

console.log("thing.nums", thing.nums);
await thing.save();

const thing2 = await Thing.findById(thing._id.toString());
console.log("thing2.nums", thing2.nums);

I get the following output:

$ node gh-15125.mjs 
Mongoose version 8.9.0
thing.nums Map(10) {
  '[0]' => [ 1 ],
  '[111]' => [ 2 ],
  '[,]' => [ 3 ],
  '[!]' => [ 4 ],
  '[111-]' => [ 5 ],
  '[-111]' => [ 6 ],
  '111]' => [ 7 ],
  '[111' => [ 8 ],
  '111' => [ 9 ],
  '0' => [ 10 ]
}
thing2.nums Map(10) {
  '0' => [ 10 ],
  '111' => [ 9 ],
  '[0]' => [ 1 ],
  '[111]' => [ 2 ],
  '[,]' => [ 3 ],
  '[!]' => [ 4 ],
  '[111-]' => [ 5 ],
  '[-111]' => [ 6 ],
  '111]' => [ 7 ],
  '[111' => [ 8 ]
}


Can you please try running the above script and see if you get the same result?

@vkarpov15 vkarpov15 added can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Jan 2, 2025
@vkarpov15 vkarpov15 removed this from the 8.9.4 milestone Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
can't reproduce Mongoose devs have been unable to reproduce this issue. Close after 14 days of inactivity.
Projects
None yet
Development

No branches or pull requests

2 participants