-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Saving modified subdocuments in multidimensional arrays throws an error #8926
Comments
Welcome @beaulac |
Sorry, I should have been clearer with the title; the issue isn't that the changes are not saved, but rather that it throws an error because it is trying to save an invalid path. @AbdelrahmanHafez
In the repro script provided, the only modification is a path on a document array. The issue seems to be that the automatic I created a branch with a test and a hacky fix (only works for 2-dimensional arrays). I'm not familiar with mongoose internals so I'm really not sure this function is even the right place to try to address it – perhaps it should be addressed by making sure the |
Oh, okay. I can confirm that this is a bug. I played with the script you provided a little bit, and can confirm that the issue is we're sending the wrong index in the 'use strict';
const mongoose = require('mongoose');
const { Schema } = mongoose;
const assert = require('assert');
run().catch(console.error);
async function run () {
await mongoose.connect('mongodb://localhost:27017/test', {
useNewUrlParser: true,
useUnifiedTopology: true
});
await mongoose.connection.dropDatabase();
const bookSchema = new Schema({ title: String });
const aisleSchema = new Schema({
shelves: [[bookSchema]]
});
const librarySchema = new Schema({ aisles: [aisleSchema] });
const Library = mongoose.model('Library', librarySchema);
await Library.create({
aisles: [{ shelves: [[{ title: 'Clean Code' }]] }]
});
const library = await Library.findOne();
library.aisles[0].shelves[0][0].title = 'Refactoring';
await library.save();
const foundLibrary = await Library.findOne();
assert.equal(foundLibrary.aisles[0].shelves[0][0].title, 'Refactoring');
console.log('All assertions passed.');
} Current output:
Expected output:
|
Do you want to request a feature or report a bug?
Bug
What is the current behavior?
Unable to save modified subdocuments if they are in a multidimensional array. Saving such a document throws an error.
The 'modified path' is missing one of the indices in the subdocument's path, and so saving throws an error because it is trying to set a property on an array which is unsupported in Mongo.
If the current behavior is a bug, please provide the steps to reproduce.
MongoError: Cannot create field 'title' in element {0: [ { _id: ObjectId('5eaf31d48ba42b1eddc904b8') } ]}
Seems the path calculated in
_markModified
incore_array.js
is missing the index of the nested array. The path is'parent.children.0.subchilds.0.title'
but should be'parent.children.0.subchilds.0.0.title'
.What is the expected behavior?
Able to save this subdocument update.
What are the versions of Node.js, Mongoose and MongoDB you are using? Note that "latest" is not a version.
Node: 12.16.3
Mongoose: 5.9.11
The text was updated successfully, but these errors were encountered: