I am trying to update a array of objects, I need to find an element with date provided and update its entry or create a element if the date does not exist.
let us say I have an object as such
{
"_id" : ObjectId("586358f9e2ce6f42cfc024d7"),
"gid" : "10",
"entries" : [
{
"date" : 20161228,
"_id" : ObjectId("586358f9e2ce6f42cfc024d8"),
"entry" : [
"fwaf",
"afwaaf",
]
}
],
"__v" : 0
}
and in this query the entry is shown as undefined(shown in console because of mongoose debug option)
Entry.findOneAndUpdate({'entries.date':20161228},{'entry':['inputis','a']},{upsert:true},function(err,data){
if (err) console.error(err)
console.log(data);
return res.json(data)
})
entries.entry does not work either. I followed this [Update nested array with Mongoose - MongoDB But I think it is not possible for me to use subdocuments, Should I restructure my database or is it fine if I keep it as such.
how can i reference the found object in mongoose? so that i can update its array?