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";
}
}