我正在使用以下版本:
PHP -> 5.6.11
MongoDB -> 3.2
MongoDB PHP Driver -> 1.1当我想安装一个MongoDB 这里驱动程序时,我注意到名为"Mongo“的驱动程序已被废弃,并按照提供的链接安装了新的扩展"MongoDB”。这将被称为php_mongodb。由于我使用的是Windows,所以我不得不将文件php_mongodb.dll复制到我的../php/ext中,并将行extension=php_mongodb.dll添加到../php/ext中
使用现在不再推荐的驱动程序,我可以插入一个MongoDate(),如下所示。
<?php
$connection = new MongoClient();
$database = $connection->selectDB('test');
$coll = new MongoCollection($database, 'users');
$coll->insert(
(object)array(
"createdAt" => new MongoDate()
)
);
?>问题是,对于新的php_mongodb,这个MongoDate()似乎不可用,我收到了错误:Fatal error: Class 'MongoDate' not found
我是否应该考虑将我的MongoDB版本降级到3.0,这样我就可以使用现在遗留的Mongo驱动程序了?
这就是我试图尝试的结果,下面的内容将向文档中添加一个Timestamp类型的值,但是,由于不是ISODate类型的值,所以无法在这个createdAt字段中强制执行TTL:
<?php
require '/vendor/autoload.php';
$date = new MongoDB\BSON\Timestamp(1, date('U'));
$manager = new MongoDB\Driver\Manager("mongodb://127.0.0.1:27017");
$collection = new MongoDB\Collection($manager, "test.users");
$ex = [
"createdAt" => $date
];
$result = $collection->insertOne( $ex );
?>我尝试过的另一件事是下面的日期,但是如果没有MongoDate()功能,我不知道如何将它作为MongoDB的类型ISODate插入:
date(DATE_ISO8601, date('U'));
发布于 2016-01-25 10:26:54
我不知道你是否还需要这个,但我一直在为这件事苦苦挣扎。最后我终于想出了办法。
$orig_date = new DateTime('2016-01-22 14:00:00');
# or you can use any other way to construct with (int timestamp)
$mongo_date = new MongoDB\BSON\UTCDateTime($orig_date->getTimestamp());
$filter = [
....
....
['timestamp' => ['$gte' => $mongo_date]],
....
....
]
# create command (for example aggregation)
$cmd = new MongoDB\Driver\Command( $filter );
# execute command
$cursor = $manager->executeCommand('my_mongo_db_name', $cmd);这个密码适用于我。
https://stackoverflow.com/questions/34447555
复制相似问题