Removing elements in a mongo array by range of indices

I’ve got a collection similar to the following structure

{
  "array1": [
     {value: "x"}
     {value: "y"}
     {value: "z"}
     {value: 0}
     {value: 1}
     {value: 2}
     {value: 3}
  ]
}

If I want to remove the last 3 elements in the array, how can this be done with a Mongo query?

Hello : )

see

The bellow code,says take the elements from [ 0 , size(array)-3]
I didn’t run it i hope will be ok.The bellow is database command,you can take the pipeline
and use it in aggregation or update.

If you want any range,like remove 5 6 7 10 12 indexes,you can reduce the array,and keep only the wanted members.

{
  "aggregate": "testcoll",
  "pipeline": [
    {
      "$addFields": {
        "array1": {
          "$slice": [
            "$array1",
            {
              "$subtract": [
                {
                  "$size": "$array1"
                },
                3
              ]
            }
          ]
        }
      }
    }
  ],
  "maxTimeMS": 300000,
  "cursor": {}
}