Watch keynotes and sessions from, our virtual developer conference.

A query to update an array within an array within a document

I’m trying to update a sub-array within a sub-array within a document, and I thought my query would work, but it doesn’t. Using the positional operator ($) works for the other data, but not for the sub-array, it just wipes whatever data is in it and makes it ‘null’.

This is my query…

await db.findOneAndUpdate({ _id: req.body.document, "stakeholders._id": req.body.subarray}, { "stakeholders.$.stakeholderTitle": req.body.subdocumentTitle, ...//lots of other stuff that works..., "stakeholders.$.subgroup": req.body.subgroup },  <-- this is the array that does not work  {
        new: true,
        runValidators: true

The ‘subgroup’ array within an array is static (it either exists or it doesn’t, and has max 8 fields of info), so it doesn’t matter if it wipes and replaces it. The docs seem to say that this isn’t possible (" The positional operator cannot be used for queries which traverse more than one array, such as queries that traverse arrays nested within other arrays, because the replacement for the placeholder is a single value"), but there must be a way to do it.

Please could someone give me a clue? Thanks.