I have been struggling with this problem for some time now. My goal is to update multiple array values in the same object with a single query.
The data in the database appears as follows:
id: 616f5aca5f60da8bb5870e36
title: "title"
recommendations: {
616f65705f60da8bb5870e37: [
0: 25,
1: "title 2"
],
61e83b48e498cc5aae9dc741: [
0: 22,
1: "title 3"
]
}
So far, I've attempted both preparing a statement like this:
const fetchQuery = "recommendations.616f65705f60da8bb5870e37.0 : 1, recommendations.61e83b48e498cc5aae9dc741.0 : 1";
const result = await videoGamesCollection.updateOne(
{ _id: ObjectId(req.body.parentId) },
{ $inc: fetchQuery }
);
And also tried using array filters:
const result = await videoGamesCollection.updateOne(
{ _id: ObjectId(req.body.parentId) },
{ $inc: {"recommendations.$[elem].0": 1} },
{arrayFilters: [{"elem":{$in:[req.body.voteIds]}}]}
);
Unfortunately, neither of these methods seem to be working. I am wondering if it is possible to achieve this or if my only option is to convert to an array of arrays instead?
Thank you!