mongodb 中數據記錄的日期格式爲
"dateCreated" : ISODate("2011-12-20T07:22:50.836Z")
通過翻閱php官網中的mongodb部分得知,要操做mongodb中的日期需要使用如下關鍵語句:
$start = new MongoDate(strtotime('-1 day'));
$end = new MongoDate(time());
$resultObject =$db->user->find(array("dateCreated"=> array('$gt' => $start,'$lte' => $end)));
【備註:SELECT * FROM users WHERE age>33 AND age<=40 相似 $db->users->find(array("age" => array('$gt' => 33, '$lte' => 40))); 】
例子:本程序實現了將mongodb中符合條件的數據放入Mysql表中。
<?php
$m = new Mongo("localhost:27017");
$db = $m->database_name;
$start = new MongoDate(strtotime('-1 day'));
$end = new MongoDate(time());
// $count =$db->user->find(array("dateCreated"=> array('$gt' => $start,'$lte' => $end)))->count();
// echo $count;
$regobj =$db->user->find(array("dateCreated"=> array('$gt' => $start,'$lte' => $end)));
foreach ($regobj as $k2)
{
$regemail="'".$k2["account"]."'";
$regdate=$k2["dateCreated"];
$regname="'".$k2["name"]."'";
$id=mysql_connect('localhost','username','passwd');
mysql_select_db('sns_tongji',$id);
$sql ="insert into sns_new_register(account,name,date) values($regemail,$regname,now())";
mysql_query("set names utf8");
if( mysql_query($sql)){
echo "insert ok\n";
}
else
{
echo mysql_error()."\n";
}
}php