0
我試圖更新我的收藏中的隨機日期。
下面的代碼是更新具有相同隨機日期的所有條目,我希望循環遍歷所有條目,併爲每個條目給出不同的日期。
我設法讓它使用索引工作。
索引大型數據集的最佳方法是什麼?mongodb php更新所有條目的隨機日期索引
$date_1 = strtotime(date('Y-m-d H:i:s'));
$date_2 = strtotime("2012-07-26 09:00:00");
$m = new Mongo();
$db = $m->comedy;
$collection = $db->cartoons;
$cursor = $collection->find();
$query = array('_id'=>array('$exists' => true));
foreach ($cursor as $obj){
$randomDate = rand($date_1,$date_2);
$collection->update(
$query,
array('$set' => array("ts"=> new MongoDate($randomDate))),
array('multiple' => true)
);
}
編輯:
foreach ($cursor as $obj){
$randomDate = rand($date_1,$date_2);
$id = $obj["_id"];
$collection->update(
array('_id'=> $id),
array('$set' => array("ts"=> new MongoDate($randomDate))),
array('multiple' => true)
);
}