I am trying to push array element into inner array of the following document structure
{
"_id" : ObjectId("599558a4a331801fe264bbb3"),
"zone" : "asia",
"country_list" : [
{
"country_name" : "india",
"state_list" : [
{
"state_name" : "kerala"
},
{
"state_name" : "tamilnadu"
}
]
},
{
"country_name" : "pak",
"state_list" : [
{
"state_name" : "ins"
}
]
}
]
}
the structure after update i want to be as
{
"_id" : ObjectId("599558a4a331801fe264bbb3"),
"zone" : "asia",
"country_list" : [
{
"country_name" : "india",
"state_list" : [
{
"state_name" : "kerala",
"city_list" : [
{
"cityname" : "tvm"
},
{
"cityname" : "kchi"
},
]
},
{
"state_name" : "tamilnadu"
}
]
},
{
"country_name" : "pak",
"state_list" : [
{
"state_name" : "ins"
}
]
}
]
}
However when I try the following code
$result = $this->collection->findOneAndUpdate(array(
"country_list.state_list.state_name"=>$this->getState()
), array(
'$push'=>array("country_list.state_list.$.city_list"=>array("city_name"=>$this->getCity()))
));
I get the error
Uncaught MongoDB\Driver\Exception\RuntimeException: cannot use the part (country_list of country_list.state_list.0.city_list) to traverse the element ({country_list: [ { country_name: "india", state_list: [ { state_name: "kerala" }, { state_name: "tamilnadu" } ] }, { country_name: "pak", state_list: [ { state_name: "ins" } ] } ]})
I am using mongo 3.4 and php 7