lykmapipo / mongoose-gridfs

mongoose gridfs on top of new gridfs api
MIT License
91 stars 39 forks source link

FileSchema.methods.write() generates a new ObjectId even when provided with one #79

Open ncroese opened 2 years ago

ncroese commented 2 years ago

When FileSchema.methods.write() is called it makes a copy of the model instance to use certain properties from, including _id. However, the method to copy the model, copyInstance() from mongoose-common, will remove the _id property from the copy, causing in a new ObjectId to be generated for the file. This results in a mismatch between the model ObjectId and the ObjectId of the file in the database.

The reason for this is that in copyInstance() a method named mergeObjects() from the common library is used, which in turn uses the compact() method. This method uses isNotValue(), which returns true for mongodb.ObjectId objects, resulting in these type of objects from being filtered from the copied instance.

I think it would be best to fix this in the isNotValue() method of the common library, making sure it returns false for mongodb.ObjectId objects.

A workaround might be to make sure to send the ObjectId to the copyInstance() method as a String type instead, or add the ObjectId to the file instance later.