I'm attempting to build a project using MongoDB which keeps track of customer data and allows the user to update that data as needed.
In my Mongoose Schema, I have an attribute called, "customerNotes" which appears as follows :
customerNotes: [
{
id:String,
text:String,
completed: Boolean,
}
]
What I would like the final product to be is a single attribute with multiple logged notes (which is why each note would have its own individual ID, Text, and whether the task has been completed yet)
Currently as I test my schema using Postman, every time I write to my API endpoint, the data I had previously written gets overwritten.
Is there a way I can improve my Mongoose Schema so that when additional Data is written to the endpoint, what was previously written is not deleted, but the new data is added to the customerNotes array, without creating an entirely new customer?
My only thought to a possible solution is to change the way the data is sent and received in the front end, but if I could have this completed in the Schema, this would work so much more efficiently.
The schema defines the type of the field, but not the permitted actions.
If the schema specifies that customerNotes
is an array of objects, it would be expected that clients could perform several actions on that field:
It is not the job of the schema to decide which of the operations are valid and which should be prohibited.
You may be able to implement middleware logic that intercepts one type of operation, such as replacing the entire array, and instead changes that to an append or merge operation.