How do I slice a nested array and how do I update a specific field in it?

Hello

The first returns the value of the field “count” for index=1 in array2

{
  _id: "some_id",
  array1: [
    {
      _id: "level_1_id",
      array2: [
        {
           _id: "level_2_id_0,
           count: 0
        },
        {
           _id: "level_2_id_1,
           count: "tomatoes"
        },
      ]
    }
  ]
}

Would print

{"countValue" : "tomatoes"}

I thought this is what you want,if its not that,if you can give sample data,and the expected output.

For the second,if you want to update 1 element of an array and you know the index of that element you can do it in many ways,bellow are 3 of possible ways.

Data(collection with 1 document for example)

{myarray [1 2 3 4 5]}

All 3 results in (at index=3 we add 10)

{myarray [1 2 3 14 5]}

The 3 methods are
1)get the member of the index,update it,and concat array_before [updated_member] array_after
you need to be careful if those exists,here it only works for index in the middle
2)map after zipmap, [[1 0][2 1][3 2] [4 3] [5 4]] zipmap array with range(count(array))
the second is the index
3)reduce array to array,and keeping the index value ,update only in that index

First is simpler,but the other 2 can be used in many things.

Slice

[
  {
    "$addFields": {
      "myarray": {
        "$let": {
          "vars": {
            "m": {
              "$arrayElemAt": [
                "$myarray",
                3
              ]
            },
            "array_before": {
              "$slice": [
                "$myarray",
                3
              ]
            },
            "array_after": {
              "$slice": [
                "$myarray",
                4,
                {
                  "$size": "$myarray"
                }
              ]
            }
          },
          "in": {
            "$let": {
              "vars": {
                "new_m": {
                  "$add": [
                    "$$m",
                    10
                  ]
                }
              },
              "in": {
                "$concatArrays": [
                  "$$array_before",
                  [
                    "$$new_m"
                  ],
                  "$$array_after"
                ]
              }
            }
          }
        }
      }
    }
  }
]

Map on zip result with range

[
  {
    "$addFields": {
      "myarray": {
        "$map": {
          "input": {
            "$zip": {
              "inputs": [
                "$myarray",
                {
                  "$range": [
                    0,
                    {
                      "$size": "$myarray"
                    }
                  ]
                }
              ]
            }
          },
          "as": "m_index",
          "in": {
            "$let": {
              "vars": {
                "m": {
                  "$arrayElemAt": [
                    "$$m_index",
                    0
                  ]
                },
                "index": {
                  "$arrayElemAt": [
                    "$$m_index",
                    1
                  ]
                }
              },
              "in": {
                "$cond": [
                  {
                    "$eq": [
                      "$$index",
                      3
                    ]
                  },
                  {
                    "$add": [
                      "$$m",
                      10
                    ]
                  },
                  "$$m"
                ]
              }
            }
          }
        }
      }
    }
  }
]

Reduce keeping the index value

[
  {
    "$addFields": {
      "myarray": {
        "$arrayElemAt": [
          {
            "$reduce": {
              "input": "$myarray",
              "initialValue": [
                [],
                0
              ],
              "in": {
                "$let": {
                  "vars": {
                    "arrray_index": "$$value",
                    "m": "$$this"
                  },
                  "in": {
                    "$let": {
                      "vars": {
                        "array": {
                          "$arrayElemAt": [
                            "$$arrray_index",
                            0
                          ]
                        },
                        "index": {
                          "$arrayElemAt": [
                            "$$arrray_index",
                            1
                          ]
                        }
                      },
                      "in": {
                        "$cond": [
                          {
                            "$eq": [
                              "$$index",
                              3
                            ]
                          },
                          [
                            {
                              "$concatArrays": [
                                "$$array",
                                [
                                  {
                                    "$add": [
                                      "$$m",
                                      10
                                    ]
                                  }
                                ]
                              ]
                            },
                            {
                              "$add": [
                                "$$index",
                                1
                              ]
                            }
                          ],
                          [
                            {
                              "$concatArrays": [
                                "$$array",
                                [
                                  "$$m"
                                ]
                              ]
                            },
                            {
                              "$add": [
                                "$$index",
                                1
                              ]
                            }
                          ]
                        ]
                      }
                    }
                  }
                }
              }
            }
          },
          0
        ]
      }
    }
  }
]