Reputation:
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
},
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));
And then I do some other stuff and exec the query with the find() command.
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) } }
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...
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.
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 } } )
No data returned.
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...
thanks!
Upvotes: 1
Views: 3543
Reputation: 23591
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.
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."
Upvotes: 1