PHP mongoDb 驱动程序,获取数据然后删除它,不起作用

发布于 2024-10-26 16:54:56 字数 357 浏览 7 评论 0原文

这是示例代码:

  $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独夜无伴 2024-11-02 16:54:57

与 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.

苯莒 2024-11-02 16:54:57

由于 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.

奶茶白久 2024-11-02 16:54:56

这对我有用。这是代码:

$coll->drop();
print("Now have ".$coll->count()." items\n");

$coll->insert(array("x" => 'blah'));
$coll->insert(array("x" => "blahblah"));
print("Inserted ".$coll->count()." items\n");

$x = $coll->findOne();
print("Object X\n");
print_r($x);
$query_x = array('_id' => $x['_id']);
$coll->remove($query_x);
print("Removed 1 item, now have ".$coll->count()." items\n");

$y = $coll->findOne($query_x);
print("Object Y\n");
print_r($y);

这是输出:

Now have 0 items
Inserted 2 items
Object X
Array
(
    [_id] => MongoId Object
        (
            [$id] => 4d8d124b6803fa623b000000
        )

    [x] => blah
)
Removed 1 item, now have 1 items
Object Y

您确定某处没有拼写错误吗?

This is working for me. Here's the code:

$coll->drop();
print("Now have ".$coll->count()." items\n");

$coll->insert(array("x" => 'blah'));
$coll->insert(array("x" => "blahblah"));
print("Inserted ".$coll->count()." items\n");

$x = $coll->findOne();
print("Object X\n");
print_r($x);
$query_x = array('_id' => $x['_id']);
$coll->remove($query_x);
print("Removed 1 item, now have ".$coll->count()." items\n");

$y = $coll->findOne($query_x);
print("Object Y\n");
print_r($y);

Here's the output:

Now have 0 items
Inserted 2 items
Object X
Array
(
    [_id] => MongoId Object
        (
            [$id] => 4d8d124b6803fa623b000000
        )

    [x] => blah
)
Removed 1 item, now have 1 items
Object Y

Are you sure there's not a typo somewhere?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文