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 · 0 comments
Open
2 tasks done

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

holem opened this issue Dec 22, 2024 · 0 comments
Labels
has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
Milestone

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue
Projects
None yet
Development

No branches or pull requests

2 participants