I am currently in the process of moving from MySQL to MongoDB for a site that I have been working on, and I am running into a strange issue.
I am trying to make the following collection:
country (string)
regions (array)
regions (string)
cities (array)
city (string)
The problem is for some reason one of the cities array's is inserting as an object. Everything is coming from the same source and using the same code.
Example of issue:
[
{
country: "United States",
regions: (array) [
{
region: "Arizona",
cities: (array) [
{ city: "Phoenix" }
]
},
{
region: "California",
cities: (object) [
{ city: "Los Angeles" }
]
}
]
}
]
The reason this is an issue because when I when I search for { "regions.cities.city": "Los Angeles" } results come back as null, but if I search { "regions.cities.city": "Phoenix" } I get the proper results.
I hope this all makes sense, but feel free to ask questions.
-- EDIT --
PHP code generating the issue:
$countries = $this->db_model->find_list('countries', array(), false, true);
foreach($countries as $key => $country)
{
if($regions = $this->db_model->find_list('regions', array('country_id' => $country['id']), false, true))
{
foreach($regions as $rkey => $region)
{
if($cities = $this->db_model->find_list('cities', array('region_id' => $region['id']), false, true))
{
$regions[$rkey]['cities'] = $cities;
}
}
$countries[$key]['regions'] = $regions;
}
}
$this->mongo->batch_insert('countries', $countries);
$regions[$rkey]['cities'] = array_values($cities);