How can i get the field Value in the nested array based on the given key

I want to sort the collection by the fieldValue based on the given fieldName .

For example: sort the collection by fieldName = ‘Author’

My problem: I am unable to get the value from the collection, like I want to add a field for authorValue.

The structure:

    [
      {
        "sections": [
          {
            name: "section1",
            fields: [
              {
                fieldName: "Author",
                fieldValue: "John"
              },
              {
                fieldName: "Movie",
                fieldValue: "Avenger"
              }
            ]
          }
        ]
      },
      {
        "sections": [
          {
            name: "section1",
            fields: [
              {
                fieldName: "Author",
                fieldValue: "Hengry"
              },
              {
                fieldName: "Movie",
                fieldValue: "Test"
              }
            ]
          }
        ]
      }
    ]

Hello @KAI_SIAN, here is a way to - sort by field value where field name is “Author”:

db.collection.aggregate([
  { $addFields: { doc: "$$ROOT" }},
  { $unwind: "$sections" }, 
  { $unwind: "$sections.fields" },
  { $match: { "sections.fields.fieldName": "Author" }},
  { $addFields: { flds: { $objectToArray: "$sections.fields" }}},
  { $unwind: "$flds" },
  { $match: { "flds.k": "fieldValue" }},
  { $sort: { "flds.v": 1 } },
  { $replaceWith: "$doc" }
])

The result is the collection’s documents sorted in ascending order, as needed - Henry comes before John.