我尝试为mongodb执行更新查询。我使用php,发送将要更新的值。我以毫秒为单位发送日期参数。但这并没有奏效。下面是代码;
function updateEventById($id1,$start,$end,$collection)
{
$this->collection = $this->database->$collection;
//$start=new MongoDate($start);
//$end=new MongoDate($end);
$newdata = array('$set' => array("start.sec" => $start,
"end.sec" => $end ));
$theObjId = new MongoId($id1["'$id"]);
$this->collection->update(array("_id" => $theObjId), $newdata);
}
在codeigniter框架下工作,所以:
$this->_comment->mongo_db->where(array("idnota" => (int)$idnota, "type" => 3))->set($update)->update($collection);
我认为问题是id需要尝试比较vardump(),看看它是否达到适当的参数
我用Mongo Shell试过你的案例,效果很好。
> db.col.insert({start: {sec: new Date()}, end: {sec: new Date()}})
> db.col.find()
{ "_id" : ObjectId("50db7b136db4ad2bff518a03"), "start" : { "sec" : ISODate("201
2-12-26T22:32:51.098Z") }, "end" : { "sec" : ISODate("2012-12-26T22:32:51.098Z")
} }
> db.col.update({_id: ObjectId("50db7b136db4ad2bff518a03")}, {$set: {"start.sec"
: new Date(), "end.sec": new Date()}})
> db.col.find()
{ "_id" : ObjectId("50db7b136db4ad2bff518a03"), "start" : { "sec" : ISODate("201
2-12-26T22:33:33.725Z") }, "end" : { "sec" : ISODate("2012-12-26T22:33:33.725Z")
} }
>
我认为你应该使用MongoDB实例的selectCollection函数,而不是调用属性(作为PyMongo的工作,扩展__getitem__
和__getattr__
):
$this->database->selectCollection($collection)->update(array("_id" => $theObjId), $newdata);
另一个问题,可能你应该调试你的代码,因为"collection"属性类可能会在执行另一个集合的更新之前改变它的值。
如果您使用的是MongoClient类,请参阅WriteConcern结果