PHP et MongoDB Driverを使用してMongoDBコレクションに日付を格納したいと思います。PHPのMongoDB isodateを正しく保存する方法
第一Connexionの:
public function process(){
try{
$this->connexion = new \MongoDB\Driver\Manager($this->dsn);
}
catch(\MongoDB\Driver\Exception\InvalidArgumentException $e){
die("Unable to connect to the MongoDB database : " . $e->getMessage());
} catch(\MongoDB\Driver\Exception\RuntimeException $e){
die("General failure : " . $e->getMessage());
}
}
第二:ここ
は私のスクリプトの一部のドキュメント作成
public function process(){
$dbConnector = dbConnector::instance(); // Instance de connexion à la base de données
$query = new \MongoDB\Driver\BulkWrite;
$query->insert($this->queryArray);
$writeConcern = new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, 1000);
//$collection = new \MongoCollection($dbConnector->connexion()->db(), $this->store->collectionName());
try{
$this->result = $dbConnector->connexion()->executeBulkWrite($dbConnector->dbName() . "." . $this->store->collectionName(), $query, $writeConcern);
} catch (\MongoDB\Driver\AuthenticationException $e){
echo "Authentication error : " . $e->getMessage() . "<br />\n";
} catch(\MongoDB\Driver\ConnextionException $e){
echo "Connexion error : " . $e->getMessage() . "<br />\n";
}
catch(\MongoDB\Driver\Exception\RuntimeException $e){
echo "Runtime error : " . $e->getMessage() . "<br />\n";
}
}
は$ queryArrayプロパティは、私が作成するキー/値ペアが含まれています。
日付を管理し、それをMongoDB ISODateに変換する変換メソッドを追加しました: \ MongoDB \ BSON \ UTCDateTimeをMongoUTCDateとして使用します。 ... ... public static function toISODate($ date){ if(is_object($ date)){ $ dateToString = $ date->年です。 " - " $ date-> monthです。 " - " $ date-> day; } else { $ dateToString = substr($ date、0,10); } $ initDate = new \ DateTime($ dateToString);
#begin_debug
#echo "Date from string " . $dateToString . " : " . $initDate->format("d-m-Y") . " (timestamp) " . $initDate->getTimestamp() . "<br />\n";
#end_debug
return new MongoUTCDate($initDate->getTimestamp());
}
私のコントローラがこのように動作します:呼び出されたとき
$stats->_id = $this->requestData()->_id;
$stats->purchases = [array(
"date" => \wp\Database\MongoDB\Utilities\MongoDate::toIsoDate($this->requestData()->date),
"coords" => array("lat" => $this->requestData()->coords->lat, "lon" => $this->requestData()->coords->lon),
"metar" => null,
"quantity" => $this->requestData()->quantity,
"price" => (float) $this->requestData()->price,
"peremption" => \wp\Database\MongoDB\Utilities\MongoDate::toIsoDate($this->requestData()->peremption)
)];
$stats->insert();
、文書が正しく作成されますが、日付が間違っている:前の例では
{
"_id" : "3256222010007",
"purchases" : [
{
"date" : ISODate("1970-01-18T07:43:01.706Z"),
"coords" : {
"lat" : 43.7294742,
"lon" : 1.416332
},
"metar" : null,
"quantity" : 1,
"price" : 2.87,
"peremption" : ISODate("1970-01-18T22:20:34.800Z")
}
]
}
、日付が日付、すなわちされるだろう...
私がデータをログすると、日付が正しくPHPで形成されていることがわかりました:
タイムスタンプを変換して、正しい日付を取得していれば、なぜMongoの日付が間違っているのか理解できません。これらの実験のために