我正在使用 PHP PECL 扩展尝试 MongoDB,但是我很难让某个更新查询正常工作。我在 SO 上四处寻找答案,但运气不佳。
我创建了一个基本集合:
$m = new Mongo;
$collection = $m->testdb->testcollection;
$collection->insert(array(
0, 1, 1, 2, 3, 5
));
Using findOne
and var_dump
记录显示如下:
array
'_id' =>
object(MongoId)[6]
public '$id' => string '4f3bde65a1f7a0315b000000' (length=24)
0 => int 0
1 => int 1
2 => int 1
3 => int 2
4 => int 3
5 => int 5
当我想使用更新时出现问题$set
。我的查询基于底部显示的映射SQL 到 Mongodb 备忘单 http://www.php.net/manual/en/mongo.sqltomongo.php在 PHP 手册中
这里我想更新字段0
重视100
$obj = $collection->findOne();
$collection->update(
array('_id' => $obj['_id']),
array('$set' => array(0 => 100))
);
重新获取记录显示保持不变.
我确实想知道我是否做错了什么_id
,但是以下更新查询does工作,尽管用新值替换整个记录,而不是简单地更新一个字段。
$collection->update(
array('_id' => $obj['_id']),
array(0 => 100)
);
对象转储:
array
'_id' =>
object(MongoId)[7]
public '$id' => string '4f3bde65a1f7a0315b000000' (length=24)
0 => int 100
有人可以指出我做错了什么以及如何正确使用$set
。我确信这是显而易见的,我只是需要第二双眼睛来关注它。
非常感谢。