I have a data similar to this:
"_id" : ObjectId("5aff8a15fac9291cb0477c87"),
"user" : ObjectId("5adbf5ffd08592252c0efeba"),
"createdTime" : ISODate("2018-05-19T02:21:09.943Z"),
"category" : [
{
"_id" : "c1bf0790-5b0b-11e8-8c5d-cd2d06b6e60a",
"catname" : "art",
"introduction" : "The world is waiting for a new way in art",
"banner" : null
},
{
"_id" : "vhjv87t8g-11e885b0b-sjhvikawik",
"catname" : "art",
"introduction" : "The world is waiting for a new way in art",
"banner" : null
}
],
When I try to query a single object in array category which looks like this
newCat = user.category.find(element => element.catname.toString() === category.catname.toString() );
Model.findByIdAndUpdate({'category._id': newCat._id}, {$set: {
"category.$.catname": catalog.catname,
"category.$.intro": catalog.introduction,
"category.$.lastUpdate": catalog.lastUpdate,
}
}, cb);
So, to get the category with catname or update the data with catname , I found out here here that I have to add _id after adding to it the result are the same it gives me the whole data and I don't want to update the whole data I want to update only the founded object. later on, I study the dot.notation is used for the array I change my variable to this
newCat = user.category.findIndex(element => element.catname.toString() === category.catname.toString() );
also the result are the same I have try many route and waste a day on this. I hope anyone can find a solution to this. Even javasceipt is a lot simpler to update array than mongoose. I am getting tired of using array in mongodb. Regards, Thanks.