How to reduce this mongodb multi update query to just one query involving nested arrays?

Schema:

{
   img: // array of minimum length 1 of ObjectIds or field is "missing" to indicate that array is empty
   tar: [
     { img:  // array of minimum length 1 of ObjectIds or field is "missing" to indicate that array is empty },
     { img:  // array of minimum length 1 of ObjectIds or field is "missing" to indicate that array is empty },
     ... 
   ]
}

This is my query to delete a photo, which is an ObjectId in the top level img array, and may be in the img field of some tar subdocuments:

      await mongo_tran_ses.withTransaction(async () => {
        // pulling the ObjectId matching `req.params._id` from `img`, and all `tar.img`
        await user.updateOne(
          { _id: ObjectId(req.user._id) },
          {
            $pull: {
              img: { _id: { $eq: ObjectId(req.params._id) } },
              "tar.$[].img": { $eq: ObjectId(req.params._id) },
            },
          },
          { session: mongo_tran_ses }
        );
        // removing all `tar.img` if it is now an empty array 
        await user.updateOne(
          {
            _id: ObjectId(req.user._id),
          },
          {
            $unset: {
              "tar.$[element].img": "",
            },
          },
          {
            arrayFilters: [{ "element.img": { $size: 0 } }],
            session: mongo_tran_ses,
          }
        );
        // removing `img` if it is now an empty array 
        await user.updateOne(
          {
            _id: ObjectId(req.user._id),
            img: { $size: 0 },
          },
          {
            $unset: {
              img: undefined,
            },
          },
          {
            session: mongo_tran_ses,
          }
        );
      }, tran_option);

The goal:

Perform the above with just one update query instead of the 3 above.

Hi :wave: @Big_Cat_Public_Safety_Act,

Could you please elaborate on it further? Also, can you confirm if your sample document looks like the following:

{
   img: [ 6423c91b114e33a0106407be, 6423c91b114e33a0106407bg, 6423c91b114e33a0106407ba]
   tar: [
     { img:  [6423c91b114e33a0106407be] },
     { img:  [6423c91b114e33a0106407be] },
     ... 
   ]
}

If not, please share a sample document, MongoDB version, and expected result document.

Could you please explain the reason behind opting for a single query? Is it to ensure atomicity? If so, would it be possible to use transactions instead?

Without knowing your use case details, I think there will be a tradeoff between performance and complexity. I believe if you can craft a three-query solution, to me it means that you can maintain it easily. A single query solution might be a little faster, but I think a proper workload simulation would be needed to determine whether the performance gains is worth it.

Regards
Kushagra

yes, your sample document is correct. It is mongodb 5.

And the motivation for one query is for faster performance. And I do understand that the query will be more complex. But I am not quite sure how much more complext.