You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
The text was updated successfully, but these errors were encountered:
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
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:
The
budgeted
field is meant to store years (keys) and an array of monthly transaction amounts (values). Here's an example: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):What is the expected behavior?
Expectation is that the database would've updated.
A few notes:
.set
method as opposed to bracket ([]
) notation (see line 4 in the code block above)..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 theMixed
schema type earlier. Could thebudgeted
field qualify as being.. aMixed
type? (My guess is no.)[Number]
). If I change that to, for example, justNumber
orString
, 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
The text was updated successfully, but these errors were encountered: