0

I am facing this problem to update subdocument in order. I have found the approach to update subdocument, even nested subdocument before. but now I have no idea how to update them in the sequence I need.

I need to update the category order in the array sequence sent, example as below

idList = ["5c603101ff83f23868bd9b12", "5c5e901e5e40d32434b6d8a0"];

The document in the collection

enter image description here

I have tried to use for loop which works, but I think there is a better approach instead of updating each subdocument one by one. Hope to get your pieces of advice

for (var i = 0; i < idList.length; i++) {
    menus.findOneAndUpdate({
        '_id': menuId,
        'categoryDetails._id': idList[i],
    }, {
        $set: {
            'categoryDetails.$.order': i
        }
    }, (err, result) => {
        if (err) {
            console.log(chalk.red(err));
            return res.status(500).json(err)
        }
        console.log(chalk.green('UPDATE: (Admin) Category order ' + i));
    });
}
return res.status(200).json()
Thomas Kim
  • 493
  • 1
  • 11
  • 17
  • See also [Matching ObjectId to String for $graphLookup](https://stackoverflow.com/a/50405010/2313887) for a coded example of constructing your update `$set` arguments and `arrayFilters` within a loop. You only need one statement to do this, not a loop of individual requests. – Neil Lunn Mar 19 '19 at 05:10
  • Hi, thanks for the reply. I have successfully use bulkWrite to achieve the result. – Thomas Kim Mar 19 '19 at 09:08
  • Does not even need `bulkWrite()` if you read the references you would have understood you just need **one** `findOneAndUpdate()` – Neil Lunn Mar 19 '19 at 09:11
  • I have read the references, but my understanding of the example is they push the operation into an array, and use bulkWrite and achieve it. I have tried bulk.find().update() too but it shows me the error of bulk.find is not defined. The error seems to came from mongoose not connected to the database before the bulk.find().update() run. So I gave up on this and tried bulkWrite. Could you please show me how to achieve with findOneAndUpdate? – Thomas Kim Mar 19 '19 at 09:16
  • How did you possibly get to Bulk writes from reading the linked duplicate? That's what I find most baffling as I gave you two links and you clearly have not read them – Neil Lunn Mar 19 '19 at 09:19
  • Sorry if I misunderstood your link, based on my little experience of MongoDB, I could only understand the first code example after the 'CONCLUSION' segment. In that example, the writer used bulk write to achieve the result. So I decided to give it a try. The graphLookup doesn't seem to solve the problem so I pass it. As from the first link, that you marked duplicate. The marked answer said the workaround is 'update each item individually (events.0.handled events.1.handled ...) or...' which is what I tried when I ask the question – Thomas Kim Mar 19 '19 at 09:26
  • Dude. You need to learn not just to accept the "accepted answer" is the thing that is actually correct. You probably should have read the comment on the accepted answer that pointed to https://stackoverflow.com/a/46054172/2313887. – Neil Lunn Mar 19 '19 at 09:31

0 Answers0