PHP Mongo 正则表达式

标签 php regex mongodb

我有如下mongo记录。

[1] => Array
    (
        [_id] => MongoId Object
            (
            )

        [id] => 195197
        [rec] => Array
            (
                [0] => Array
                    (
                        [id] => 195197
                        [data] => ways to skin a cat
                        [total] => 313
                    )

                 [1] => Array
                    (
                        [id] => 702724
                        [data] => 2010-07-25 15:09:40
                    )
            )

          [rec2] => Array
            (
                [0] => Array
                    (
                        [id] => 195197
                        [data] => ways to skin a cat
                        [total] => 313
                    )

                 [1] => Array
                    (
                        [id] => 702724
                        [data] => 2010-07-25 15:09:40
                    )
            )
    );

我想通过 Mongo 正则表达式从具有 rec 和 rec2 元素且“数据”元素采用日期格式的 mongo 数据库中搜索那些记录。有什么办法可以找到这样的记录吗?

最佳答案

在 shell 中,您应该能够执行如下操作:

db.foo.find( { 'rec.data' : /\d\d\d\d-\d\d-\d\d/ } )

在 PHP 中,您将使用 MongoRegex class .

看起来像这样:

$regex = new MongoRegex("/\d\d\d\d-\d\d-\d\d/");
$where = array("rec.data" => $regex);
$cursor = $db->foo->find( $where ); 

关于PHP Mongo 正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5064611/

相关文章:

php - mysql 索引到日期时间类型的列

php - 尝试使用导入器重新上传图像,但它仍然检测到已删除的图像

PHP pcntl模块安装

javascript - 使用 Regex/jQuery 匹配 HTML 属性(用于搜索)

使用集群 mongo 实例时 mongodb i/o 超时

php - 在不提交表单的情况下获取选择框的值

html - 正则表达式 VBA 匹配

python - 需要帮助了解如何在 Python 中使用不太复杂的正则表达式

mongodb - 类型重命名后如何使用 Spring Data MongoDB 类型信息更新现有文档?

algorithm - MongoDB 查询按时间顺序更新总和