$Set an array of documents using dot notation resulting in ambiguous behaviour

I am using $set to perform an update/upsert

{
  "company": {
    "name": "Google"
  },
  "countries": [
    {
      "name": "India",
      "cities": [
        {
          "name": "Bengaluru"
      ]
    }
  ]
}

This is my update query using $set to set array of documents. My understanding is dot notation with index number as a separator should treat these fields as an array

db.queries.update(
  {
    "_id": ObjectId("65320deff43afadac2cce910")
  },
  {
    $set: {
      "countries.0.cities.0.name": "Bengaluru",
      "countries.0.name": "India",
      "company.name": "Google"
    }
  },
  {upsert:true}
)

My expectation in DB

{
  _id: ObjectId("65320deff43afadac2cce910"),
  company: { name: 'Google'},
  countries: [
    {
      name: 'India'
      cities: [
        { name: 'Bengaluru'}
      ]
    }
  ]
}

Actual entry in DB

{
  '_id': ObjectId("65320deff43afadac2cce910"),
  'company': { name: 'Google'},
  'countries': {
    '0' : {
      'name': 'India'
      "cities': {
        '0': {
          'name': 'Bengaluru'
        }
      }
    }
  }
}

Expected countries and countries.cities to be set as arrays. However, they were interpreted as object and set accordingly in db.

Kindly help understanding this.

As far as I know, for the dot notation to work with array indexes, the array has to exists. Yours is an edge case where it does not work because the array does not exist before the update.

For example:

c.insertOne( { _id : 2 , "countries" : []})
c.updateOne( { _id : 2 } , { $set : { "countries.4.name" : "369"}})
c.findOne( { _id : 2 } )
/* result, notice that the non-existing entries becomes null */
{
  _id: 2,
  countries: [
    null,
    null,
    null,
    null,
    {
      name: '369'
    }
  ]
}