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

Updating an array inside a map does not get properly saved #9813

Closed
samsam1414 opened this issue Jan 14, 2021 · 1 comment
Closed

Updating an array inside a map does not get properly saved #9813

samsam1414 opened this issue Jan 14, 2021 · 1 comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Milestone

Comments

@samsam1414
Copy link

Do you want to request a feature or report a bug?

A bug.

What is the current behavior?

When updating a map (via .set), the result does not get saved in MongoDB.

If the current behavior is a bug, please provide the steps to reproduce.

Below is an example schema I'm working with, representing part of a money budgeting program:

const BudgetSchema = new mongoose.Schema({
  budgeted: {
    type: Map,
    of: [Number]
  }
});

const Budget = mongoose.model('Budget', BudgetSchema );

The budgeted field is meant to store years (keys) and an array of monthly transaction amounts (values). Here's an example:

{
   "budgeted": {
      "2020": [100, 200, 300, 400, 500, 600, 700, 800, 900, 1000, 1100, 1200],
      "2021": [101, 201, 301, 401, 501, 601, 701, 801, 901, 1001, 1101, 1201],
            // ^ one value for each month of the year
   }   
}

When I try to update one of these array values, the .save method is successful... but the document does not actually get updated in the database. Here's an example (where I've just hard-coded values):

const budget = await Budget.findById('600000023b392210a8bb6011');

const budgeted = budget.budgeted.get('2020');   // get budgeted array for 2020
budgeted.set(2, 10);                            // set March's value to 10
budget.budgeted.set('2020', budgeted);          // save budgeted array into 2020

console.log(budget.modifiedPaths());            // this prints out [ 'budgeted', 'budgeted.$*', 'budgeted.$*.2' ]

const updatedBudget = await budget.save();      // returns updated budget.. but the database hasn't actually updated!

What is the expected behavior?

Expectation is that the database would've updated.

A few notes:

  • I've seen the Mongoose FAQ -- particularly the 1st question -- that mentions setting array indices directly. As a result, I'm using the .set method as opposed to bracket ([]) notation (see line 4 in the code block above).
  • I realized I could use .markModified to manually tell Mongoose about this update. However, I was hoping to avoid this, unless this particular use-case definitively needs it. On a related note, I read about the Mixed schema type earlier. Could the budgeted field qualify as being.. a Mixed type? (My guess is no.)
  • I have a hunch that the issue is related to the array that's being used as the values in the map (e.g. [Number]). If I change that to, for example, just Number or String, things.. just work! But still wondering what it is with arrays that's causing issues.

What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.

Node.js: 14.13.1
Mongoose: 5.11.11
MongoDB: 4.2.3

@vkarpov15 vkarpov15 added this to the 5.11.13 milestone Jan 15, 2021
@vkarpov15 vkarpov15 added the has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue label Jan 15, 2021
@vkarpov15 vkarpov15 added confirmed-bug We've confirmed this is a bug in Mongoose and will fix it. and removed has repro script There is a repro script, the Mongoose devs need to confirm that it reproduces the issue labels Jan 19, 2021
vkarpov15 added a commit that referenced this issue Jan 19, 2021
@samsam1414
Copy link
Author

Thank you @vkarpov15. Can confirm my application is working exactly as intended.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
confirmed-bug We've confirmed this is a bug in Mongoose and will fix it.
Projects
None yet
Development

No branches or pull requests

2 participants