我希望对MongoDB查找查询执行REGEX,该查询仅查找字符串中带有图像的任何内容,其搜索是文件类型标头,因此它通常看起来像图像/png或文本/html。
我正在为正则表达式运行此代码:
array('fileType'=>array('$regex'=>'^image'))
而这段代码:
foreach ($this->grid->find(array('fileType'=>array('$regex'=>'^image'))) as $file) {
$id = (string) $file->file['_id'];
$filename = htmlspecialchars($file->file["filename"]);
$filetype = isset($file->file["filetype"]) ? $file->file["filetype"] : 'application/octet-stream';
if($filetype == 'image/'.$chosenfile.''){
$links[] = sprintf('<img src="lib/download.php?id=%s" height="200px" width="200px">', $id);
}elseif($chosenfile == ''){
$links[] = sprintf('<img src="lib/download.php?id=%s" height="200px" width="200px">', $id);
}
}
这是你的问题:
array('$regex'=>'^image')
它应该使用MongoRegex对象:
array('fileType' => new MongoRegex('/^image/i'))
文档定义如下:http://www.php.net/manual/en/class.mongoregex.php
现在行得通吗?
检查下面的MongoDB实例查询使用php它的工作。
$pipeline = array(
array('$match' => array(
'$and' => array(array('data_UTC' => array('$gte' => new MongoDate(strtotime('2017-01-20T00:00:00-02:00')),
'$lt' => new MongoDate(strtotime('2017-01-29T00:00:00-02:00'))
),
'carga' => array('regex' => new MongoRegex('/^soja/i'))
)
)
)
),
array('$group' => array('_id' => array('$dataToString' => array('format' => '%Y-%m-%d', 'date' => array('$subtract' => array('$data_UTC', 1000 * 60 * 60 * 2)))), 'qtd_acessos' => array('$sum' => 1 ))),
array('$sort' => array('_id' => -1)),
array('$limit' => 50)
);
$results = $this->mongodb->aggregate($pipeline);