是否可以通过匹配记录中的关键字或日期范围来查询mongodb?
例如,如果我有一个集合,其中包含"标题","作者"和"日期"。我有这个记录标题为"你好世界",作者为" john billy",日期为" 2014/05/12"。
是否可以通过输入" world"来返回此记录。或者"比利"或日期范围(2014/01/12至2014/06/12)
如何编写查询以获取我想要的记录?
提前致谢!
下面是我的日期代码:$ from_Id是
$rangeQuery = array('timestamp' => array( '$gte' => $from_Id, '$lte' => $to_Id ));
$cursor = $collection->find($rangeQuery);
答案 0 :(得分:1)
您必须使用$lt
/ $lte
(对应于< /< =)和$gt
/ $gte
(对应于> / > =)运算符在查询中设置日期范围。只需查看mongodb文档:
http://docs.mongodb.org/manual/reference/operator/query-comparison/
以下是php doc(http://php.net/manual/en/mongocollection.find.php)的一个例子:
<?php
$m = new MongoClient();
$db = $m->selectDB('test');
$collection = new MongoCollection($db, 'phpmanual');
// search for documents where 5 < x < 20
$rangeQuery = array('x' => array( '$gt' => 5, '$lt' => 20 ));
$cursor = $collection->find($rangeQuery);
foreach ($cursor as $doc) {
var_dump($doc);
}
?>
@SaTya提供的链接将帮助您使用关键字进行搜索。