PHP mongoDb 驱动程序,获取数据然后删除它,不起作用
这是示例代码:
$r = $coll->findOne();
$coll->remove(array("_id"=>$r["_id"])); // use the same object id as retreived from DB
$ret=$coll->findOne(array("_id"=>($r["_id"])));
var_dump($ret); // dumps the records that was supposed to be deleted
集合中的记录具有 MongoDB objectId,而不是字符串。 控制台上的相同逻辑工作正常并正确删除了记录。
This is the sample code :
$r = $coll->findOne();
$coll->remove(array("_id"=>$r["_id"])); // use the same object id as retreived from DB
$ret=$coll->findOne(array("_id"=>($r["_id"])));
var_dump($ret); // dumps the records that was supposed to be deleted
The records in the collection have MongoDB objectId, and not strings.
Same logic on console works fine and deleted the record correctly.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
与 php == 运算符不同,mongo 的相等运算符始终使用“对象相等”,这类似于 php 的相同比较运算符 (===) 或 java 的 .equals()。虽然您的代码看起来应该可以工作(并且对于我来说,使用测试数据集确实可以正常工作),但您的数据集的某些内容可能会导致 php 将返回的 MongoId 转换为字符串。 在此处了解有关 MongoId 的更多信息。
通过对查询本身执行 var_dump,确保您的查询提供 MongoId 进行比较。另外,请确保您运行的是最新版本的 PHP Mongo 驱动程序。
Unlike the php == operator, mongo's equality operator always uses "Object equality" which is like php's identical comparison operator (===) or java's .equals(). While your code looks as though it should work (and it does work fine for me with a test dataset), something about your dataset may be causing php to cast the returned MongoId to a string. Read more about MongoId here.
Make sure that your query is supplying a MongoId for comparison by doing a var_dump of the query itself. Also, make sure that you are running the latest version of the PHP Mongo driver.
由于 PHP 是松散类型的,因此最重要的是确保将所有输入值和搜索值转换为预期且一致的数据类型,否则它肯定无法找到您预期的文档。
在 PHP 中使用 MongoDB 时,我会有意地转换所有内容,以避免任何可能的混乱或错误。
此外,groups.google.com 上的 mongodb 用户组非常好且反应灵敏,因此如果您还没有使用该资源,我肯定会考虑加入它。
Since PHP is loosely typed it is most important to ensure that you cast all input values and search values to the expected and consistent data type otherwise it will surely not locate your expected document.
While using MongoDB within PHP I make it a point to cast everything intentionally in order to avoid any possible confusion or error.
Also, the mongodb-user group at groups.google.com is very good and responsive so if you are not already utilizing that resource I would definitely consider joining it.
这对我有用。这是代码:
这是输出:
您确定某处没有拼写错误吗?
This is working for me. Here's the code:
Here's the output:
Are you sure there's not a typo somewhere?