A
A
Arseniy2014-07-30 11:44:26
PHP
Arseniy, 2014-07-30 11:44:26

Why does an array disappear during a document update in mongoDB?

During the first write to the document, the following structure is obtained:

{
   "_id": "ObjectId("53d8ae568c98c83410000003")",
   "city": "nsk",
   "date": "ISODate("2014-07-29T20: 00: 00.0Z")",
   "forecast_now": {
     "time": 1406709334,
     "current_temp_c": "21,2",
     "cloud_title": "ясная погода",
     "precip_title": "без осадков" 
  },
   "forecast_archieve": [
     
  ] 
}

The bottom line is that next time I need to change the contents of forecast_now, and send what was in it to the archive. That is, ideally, I wanted to get something like this:
{
   "_id": "ObjectId("53d8ae568c98c83410000003")",
   "city": "nsk",
   "date": "ISODate("2014-07-29T20: 00: 00.0Z")",
   "forecast_now": {
     "time": 1406709479,
     "current_temp_c": "21,2",
     "cloud_title": "ясная погода",
     "precip_title": "без осадков" 
    },
   "forecast_archieve": [
     {
       "time": 1406709334,
       "current_temp_c": "21,2",
       "cloud_title": "ясная погода",
       "precip_title": "без осадков" 
    } 
   ] 
}

But instead I get this structure:
{
   "_id": "ObjectId("53d8ae568c98c83410000003")",
   "city": "nsk",
   "date": "ISODate("2014-07-29T20: 00: 00.0Z")",
   "forecast_archieve": {
     "0": {
       "time": 1406709334,
       "current_temp_c": "21,2",
       "cloud_title": "ясная погода",
       "precip_title": "без осадков" 
    } 
  },
   "forecast_now": {
     "time": 1406709479,
     "current_temp_c": "21,2",
     "cloud_title": "ясная погода",
     "precip_title": "без осадков" 
  } 
}

The update code looks like this:
$tmp_arr = $result["forecast_now"];
if (!is_null($tmp_arr)){
  $new_doc = array ('$set' => array("forecast_now" => $arr), '$push' => array("forecast_archieve" => $tmp_arr));
  $collection->update($result, $new_doc);
}

Please tell me how to fix this, thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kliss, 2014-08-11
@kliss

The solution is simple: don't use pohape and its array/hash dualism.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question