MongoDb更新在php中不适用于date

MongoDb更新在php中不适用于date,php,mongodb,Php,Mongodb,我尝试对Mongo Db执行更新查询。我使用php,并发送要更新的值。我以毫秒为单位发送日期参数。但它不起作用。这是代码 function updateEventById($id1,$start,$end,$collection) { $this->collection = $this->database->$collection; //$start=new MongoDate($start); //$end=new MongoDate($e

我尝试对Mongo Db执行更新查询。我使用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的工作原理一样,扩展了
\uuuuuuGetItem\uuuuuuuuuuu
\uuuuuuuuu getattr\uuuuuuuuuuuuuu
):

另一个问题是,您可能应该调试代码,因为“collection”属性类可能会在执行另一个集合中的更新之前更改其值


如果您使用的是MongoClient类,请参阅WriteConcern结果。

它获取id,但会在日期值中插入非有效日期。我没有使用任何框架什么不起作用,注释掉的代码?告诉我们它是如何失效的,我们可以告诉你原因。还有,为什么您要更新
start.sec
您很想用
MongoDate
更新
start
$this->database->selectCollection($collection)->update(array("_id" => $theObjId), $newdata);