This is the structure of objects in database. I need nested schemas because nested objects of the array could be updated in different time and I need to know if this happens.
const PublicationSchema = new Schema({
id: String,
data: String,
}, {timestamps: true, _id: false]);
const AuthorSchema = new Schema({
id: String,
publications: [PublicationSchema]
}, { timestamps: true, _id: false});
My thought process is if I find the element in the array set it
AuthorModel.findOneAndUpdate(
{
id: authorId,
"publications.id": publicationId
},
{
$set: {"publications.$[pub]": theNewObject
},
{
arrayFilters: [{ 'publication.id': publicationId }],
new: true
},
and I get this error:
Updating the path 'publications.$[pub].updatedAt' would create a conflict at 'publications.$[pub]'
I want to mention that if I had:
const AuthorSchema = new Schema({
id: String,
publications: [Schema.Type.Mixed]
}, { timestamps: true, _id: false});
then my function is working without errors, but doesn't include the updatedAt field. So how to make this update work with nested schemas?
Note: even after reading Similar Question, Similar Question2 I can't understand why this error occurs.