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.