PHP和MongoDate查询 [英] PHP and MongoDate query

查看:42
本文介绍了PHP和MongoDate查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PHP脚本创建一个mongo集合,该脚本带有一个用MongoDate初始化的子字段.集合中的结果字段如下所示:

Created a mongo collection using a PHP script with a sub-field initialized with MongoDate. The resulting field in the collection looks like this:

"ts_add" : {
    "sec" : 1335468966,
    "usec" : 420000
},

当我在PHP中基于该字段构建查询时,我将像这样构建它:

When I am building my query aginst this field in PHP, I am building it like this:

$val = new MongoDate(strtotime($strDate)); //  $strDate = '2012-04-25'
...
$aryQuery = array(STRING_COL_NAME => array ('$gte' => $val));

然后我做其他一些事情,并使用find()命令执行查询.

And then I do some other stuff and exec the query with the find() command.

根据调试器,PHP中的构建查询结构如下所示:

The build query structure in PHP looks like this, according to the debugger:

find(Array
(
    [ts_add] => Array
        (
            [$gte] => MongoDate Object
                (
                    [sec] => 1335337200
                    [usec] => 0
                )

        )
)

在我的日志文件中,我看到以下内容:

In my log files, I see this:

runQuery called coll.table { ts_add: { $gte: new Date(1335337200000) } }

但是从来没有返回任何数据....我有点被所有额外的零所迷惑,但是我认为这是默认时间戳数据的添加或某种怪异的MongoDate-ism ...

But no data is ever returned....and I'm kind of weirded-out by all the extra zeros but I am thinking that's default timestamp data addded or some weird MongoDate-ism...

如果我从cli手动运行此命令:

If I manually, from the cli, run this command:

> db.table.find({ "ts_add.sec": { $gte:1335337200 } })

返回完整的数据集(按预期).

The full data set (as expected) is returned.

然后尝试从cli尝试模仿MongoDate:

Tried this, then, from the cli to try to mimic the MongoDate thing:

> var start = new Date(2012, 3, 10)
> db.addons_add.find({ ts_add : { $gte : start } } )

没有返回数据.

如果我使用相同的输入数据并将其转换为MongoID,并针对$ _id字段进行搜索,则搜索成功.

If I use the same input data and convert it to a MongoID, searching against the $_id field, the search is successful.

有什么建议吗?我想念什么?有种感觉,我正站在树上三英寸处,抱怨我没看到森林...

Any suggestions? What am I missing? Have the feeling I'm standing three-inches from the tree complaining about how I'm not seeing the forest...

谢谢!

推荐答案

mongod以JavaScript格式打印所有内容,以毫秒为单位打印日期(这是所有多余的0的来源).因此,正确查询1335337200会变成1335337200 * 1000 = 1335337200000.

mongod prints everything in JavaScript format, which prints dates in milliseconds (which is where all the extra 0s are coming from). So querying for 1335337200 correctly turns into 1335337200 * 1000 = 1335337200000.

但是,您粘贴的初始文档片段看起来是错误的. "ts_add" : {"sec" : 1335468966, "usec" : 420000}是JSON,但是Date类型在JavaScript中看起来不应该这样.您如何保存日期?看起来它们正在转换为另一种类型的对象,然后存储为通用对象"而不是日期类型".

The initial document fragment you pasted looks wrong, though. "ts_add" : {"sec" : 1335468966, "usec" : 420000} is JSON, but the Date type shouldn't look like that in JavaScript. How are you saving dates? It looks like they're being converted to another type of object and then stored as "generic object" instead of "date type."

这篇关于PHP和MongoDate查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆