当我尝试在mongodb上获取数据时,我遇到了错误

I have many stats data like this:

enter image description here

I have face to get this error when i get this data in controller:

Could not convert array to a date value 

Stacktrace:

    Stack Trace
in vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/Types/DateType.php at line 67   -
        }
        if ($datetime === false) {
            throw new \InvalidArgumentException(sprintf('Could not convert %s to a date value', is_scalar($value) ? '"'.$value.'"' : gettype($value)), 0, $exception);
        }
        return $datetime;
at DateType ::getDateTime (array('date' => '2014-07-16 13:39:01.000000', 'timezone_type' => '3', 'timezone' => 'Europe/Istanbul')) 
in app/cache/dev/doctrine/odm/mongodb/Hydrators/OjsAnalyticsBundleDocumentObjectViewsHydrator.php at line 81   +
at OjsAnalyticsBundleDocumentObjectViewsHydrator ->hydrate (object(ObjectViews), array('_id' => object(MongoId), 'entity' => 'article', 'objectId' => '116597', 'logDate' => array('date' => '2014-07-16 13:39:01.000000', 'timezone_type' => '3', 'timezone' => 'Europe/Istanbul')), array()) 
in vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/Hydrator/HydratorFactory.php at line 453   +
at HydratorFactory ->hydrate (object(ObjectViews), array('_id' => object(MongoId), 'entity' => 'article', 'objectId' => '116597', 'logDate' => array('date' => '2014-07-16 13:39:01.000000', 'timezone_type' => '3', 'timezone' => 'Europe/Istanbul')), array()) 
in vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/UnitOfWork.php at line 2843   +
at UnitOfWork ->getOrCreateDocument ('Ojs\AnalyticsBundle\Document\ObjectViews', array('_id' => object(MongoId), 'entity' => 'article', 'objectId' => '116597', 'logDate' => array('date' => '2014-07-16 13:39:01.000000', 'timezone_type' => '3', 'timezone' => 'Europe/Istanbul')), array()) 
in vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/Cursor.php at line 224   +
at Cursor ->current ()
at iterator_to_array (object(Cursor), false) 
in vendor/doctrine/mongodb/lib/Doctrine/MongoDB/Cursor.php at line 628   +
at Cursor ->Doctrine\MongoDB\{closure} () 
in vendor/doctrine/mongodb/lib/Doctrine/MongoDB/Cursor.php at line 660   +
at Cursor ->retry (object(Closure), true) 
in vendor/doctrine/mongodb/lib/Doctrine/MongoDB/Cursor.php at line 629   +
at Cursor ->toArray (false) 
in vendor/doctrine/mongodb-odm/lib/Doctrine/ODM/MongoDB/DocumentRepository.php at line 173   +
at DocumentRepository ->findBy (array('entity' => 'article', 'objectId' => '116597')) 
in src/Ojs/Common/Services/JournalService.php at line 338   +
at JournalService ->getArticleStats ('116597', object(Journal)) 
in src/Ojs/ReportBundle/Controller/AnalyticsReportController.php at line 66   +
at AnalyticsReportController ->detailAction ('116597')
at call_user_func_array (array(object(AnalyticsReportController), 'detailAction'), array('116597')) 
in app/bootstrap.php.cache at line 3094   +
at HttpKernel ->handleRaw (object(Request), '1') 
in app/bootstrap.php.cache at line 3056   +
at HttpKernel ->handle (object(Request), '1', true) 
in app/bootstrap.php.cache at line 3207   +
at ContainerAwareHttpKernel ->handle (object(Request), '1', true) 
in app/bootstrap.php.cache at line 2429   +
at Kernel ->handle (object(Request)) 
in web/app_dev.php at line 31   +

Related field mapping in MongoDB mapping file:

 /**
     * @MongoDB\Date
     */
    public $logDate;
/**
     * Get logDate
     *
     * @return @MongoDb\Date $logDate
     */
    public function getLogDate()
    {
        return $this->logDate;
    }

    /**
     * Set logDate
     *
     * @param  $logDate
     * @return self
     */
    public function setLogDate($logDate)
    {
        $this->logDate = $logDate;

        return $this;
    }

First of all, i ask 2 questions about this things.

  • Why mongodb storing date data as an object/array ?
  • How can i fix this shit ?

thanks for all interests.

logDate is an array because it contains the following properties date, timezone_type and timezone. No idea why MongoDB is doing that I have never really worked with it so I do not have an answer to that question. However, if you want to handle timezone information as well you can just format the date into ISO8601 and store that then cross reference the given timezone for the actual name of the timezone.

To fix the logDate issue you can just use it as an array e.g. logDate['date'] to get the actually date time.