查询mongodb 3.6集合,用于过去24小时内创建的文档

时间:2018-02-16 05:43:12

标签: php mongodb

查询在过去24小时内在php中创建的文档的mongodb 3.6集合。

集合

{
    "_id" : ObjectId("5a85b8d7a7c1cb1f380000a6"),
    "userid" : "matt",
    "post" : "the value of human life",
    "message" : [ ],
    "imgsrc" : "/images/pic.jpg",
    "state" : "lagos",
    "repost" : [ ],
    "updated" : "2018-02-15 17:44:07",
    "created" : "2018-02-15 17:44:07"
}

1 个答案:

答案 0 :(得分:0)

使用mongodb聚合,

use MongoDB;
$this->connection = new MongoDB\Client("mongodb://localhost:27017");
private $dbname = 'database';
private $db;
private $collection;
$this->db = $this->connection->{$this->dbname};
$this->collection = $this->db->collection;


$date = Date("Y-m-d H:i:s", strtotime("-24 hours"));
$pipeline = array(
['$match' =>[
    ['created' =>['$gt'=> $this->timedate->setDate($date)]]
]                
]);
$query = $this->collection->aggregate($pipeline);
return $query->toArray();