当前位置: 首页 > 知识库问答 >
问题:

如何正确存储MongoDB isodate从PHP脚本

党星鹏
2023-03-14

我想使用PHP et MongoDB驱动程序在MongoDB集合中存储日期。

以下是我的部分脚本:

第一连接:

    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。。。公共静态函数toISODate($date){if(is_object($date)){$dateToString=$date-

    #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中的格式是正确的:

Date from string 2017-06-04 : 04-06-2017 (timestamp) 1496527200<br />

如果转换回时间戳,得到了正确的日期...所以,不明白为什么蒙戈日期是不正确的。

共有1个答案

曾修真
2023-03-14

对于那些实验相同的问题,只需乘以1000 PHP给出的时间戳:

    public static function toISODate($date){
    if(is_object($date)){
        $dateToString = $date->year . "-" . $date->month . "-" . $date->day;
    } else {
        $dateToString = substr($date,0,10);
    }
    $initDate = new \DateTime($dateToString);

    #begin_debug
    #echo "Date récupérée à partir de " . $dateToString . " : " . $initDate->format("d-m-Y") . " (timestamp) " . $initDate->getTimestamp() . "<br />\n";
    #end_debug
    $stampedDate = $initDate->getTimestamp() * 1000;

    $mongoUTCDate = new MongoUTCDate($stampedDate);

    return $mongoUTCDate;
}

正确的日期已存储。。。

 类似资料:
  • 我一直在研究散列/加密密码并将其存储在数据库中的正确方法。我知道盐和散列,所以我环顾四周,PBKDF2似乎是一个不错的选择。所以我找到了这个网站,它提供了一个很好的教程,以及一个适用于PHP的PBKDF2(这是我在我的网站上使用的)。 因此,我设置了我的网站,以使用这些功能生成/创建密码,但正如您在以下代码中看到的: salt在create_散列函数中生成,并存储在生成的散列中,该散列最终看起来像

  • 前言:我试图在关系数据库的MVC体系结构中使用存储库模式。 我最近开始学习PHP中的TDD,我意识到我的数据库与应用程序的其余部分耦合得太紧密了。我读过关于存储库和使用IoC容器将其“注入”到我的控制器中的文章。很酷的东西。但现在有一些关于存储库设计的实际问题。请考虑以下示例。 所有这些查找方法都使用选择所有字段()方法。然而,在我的应用程序中,我总是试图限制我得到的字段的数量,因为这经常增加开销

  • 似乎可以从Android(本机)访问;但我仍在努力让这一切顺利进行。 在我的React本机应用程序中,我有如下内容: 百货商店 用户减速器 从商店获取用户 现在的问题开始时,我试图让相同的用户从Android,我所有的挫折尝试这是我有: 由于用户认为它是一个对象,我不知道这是否是获取的正确方法,我尝试获取,但没有成功。 我开始认为我应该使用react原生sqlite存储,在那里我可以知道我的数据结

  • 问题内容: 前言:我正在尝试在MVC体系结构和关系数据库中使用存储库模式。 我最近开始学习PHP中的TDD,并且意识到我的数据库与我的其余应用程序之间的联系太紧密了。我已经阅读了有关存储库并使用IoC容器将其“注入”到控制器中的信息。很酷的东西。但是现在有一些关于存储库设计的实际问题。考虑以下示例。 问题1:字段过多 所有这些查找方法均使用全选()方法。但是,在我的应用程序中,我总是试图限制获得的

  • 问题内容: 我想看看确定python中当前脚本目录的最佳方法是什么? 我发现由于调用python代码的方式很多,很难找到一个好的解决方案。 这里有一些问题: 如果脚本与执行没有定义 仅在模块中定义 用例: ./myfile.py python myfile.py ./somedir/myfile.py python somedir/myfile.py execfile(‘myfile.py’) (

  • 我是Cassandra数据库的初学者。我准备了事件存储表的示例,如下所示: 哪里: 身份证- 数据- 版本- 聚合ID- 事件身份- 日期- 我不确定我的主键是否正确(Aggregate Id,版本)以及按版本聚类。我想知道我的表是否会被正确分区。Aggregate Id分区,其中包含按版本排序的此聚合的所有事件。