为什么使用空条件时 $pull 运算符在 MongoDB 上不起作用?

发布于 2024-12-09 09:51:38 字数 1351 浏览 7 评论 0原文

我有这样的节点:

[_id] => MongoId Object (
    [$id] => 4e90cb3cd68417740c000017
)
[label] => mystery
[owner] => me
[parents] => Array (
    [0] => Array (
        [id] => MongoId Object (
            [$id] => 4e8c6bb6d68417340e0004ca
        )
        [owner] => userid
        [timestamp] => 1318112522
    )
)
[timestamp] => 1318112060
[translabel] => mystery
[type] => 0

我想做的是删除 id 为 4e8c6bb6d68417340e0004ca 的父母,无论他们在哪里。

例如,这应该可以工作(最新的 Mongo):

db.nodes.update({},{$pull : {"parents": { "id" : ObjectId("4e8c6bb6d68417340e0004ca") }}});

或者在 PHP 中同样有效(最新的驱动程序等):

$mongodb->nodes->update(array(),array('$pull'=> array('parents'=>array('id'=> new MongoId("4e8c6bb6d68417340e0004ca")))));

两者都不做任何事情!


另一方面:

db.nodes.update({"_id": ObjectId("4e90cb3cd68417740c000017")},{$pull : {"parents": { "id" : ObjectId("4e8c6bb6d68417340e0004ca") }}});

或者在 PHP 中同样:

$mongodb->nodes->update(array('_id' => new MongoId("4e90cb3cd68417740c000017"),array('$pull'=> array('parents'=>array('id'=> new MongoId("4e8c6bb6d68417340e0004ca")))));

工作得很好!这是一个错误吗?我在子文档中的 MongoID 对象中使用“id”而不是“_id”是否有问题?预先感谢您的任何帮助!

I have nodes like this:

[_id] => MongoId Object (
    [$id] => 4e90cb3cd68417740c000017
)
[label] => mystery
[owner] => me
[parents] => Array (
    [0] => Array (
        [id] => MongoId Object (
            [$id] => 4e8c6bb6d68417340e0004ca
        )
        [owner] => userid
        [timestamp] => 1318112522
    )
)
[timestamp] => 1318112060
[translabel] => mystery
[type] => 0

What I am trying to do is to remove parents with id : 4e8c6bb6d68417340e0004ca , wherever they are.

For example this should have been working (latest Mongo):

db.nodes.update({},{$pull : {"parents": { "id" : ObjectId("4e8c6bb6d68417340e0004ca") }}});

or equaly in PHP (latest driver etc):

$mongodb->nodes->update(array(),array('$pull'=> array('parents'=>array('id'=> new MongoId("4e8c6bb6d68417340e0004ca")))));

both don't do anything!


on the other hand:

db.nodes.update({"_id": ObjectId("4e90cb3cd68417740c000017")},{$pull : {"parents": { "id" : ObjectId("4e8c6bb6d68417340e0004ca") }}});

or equaly in PHP:

$mongodb->nodes->update(array('_id' => new MongoId("4e90cb3cd68417740c000017"),array('$pull'=> array('parents'=>array('id'=> new MongoId("4e8c6bb6d68417340e0004ca")))));

work perfectly well! Is this a bug? Is it a problem that I use "id" instead of "_id" with MongoID objects in my subdocuments? Thanks in advance for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

尬尬 2024-12-16 09:51:38

您是否尝试过在更新命令

db.collection.update( criteria, objNew, upsert, multi )

multi 上启用 多标志 -指示是否应更新所有符合条件的文档,而不仅仅是一个。与下面的 $ 运算符一起使用可能很有用。

并将您的查询更改为

db.nodes.update({},{$pull : {"parents": { "id" : ObjectId("4e8c6bb6d68417340e0004ca") }}},false,true);

,或者

db.nodes.update({ "_id" : { $exists : true } },{$pull : {"parents": { "id" : ObjectId("4e8c6bb6d68417340e0004ca") }}},false,true);

我自己没有测试过代码,但我确信以上任何一项都可以工作。

Have you tried enable multi flag on update command

db.collection.update( criteria, objNew, upsert, multi )

multi - indicates if all documents matching criteria should be updated rather than just one. Can be useful with the $ operators below.

and change your query to

db.nodes.update({},{$pull : {"parents": { "id" : ObjectId("4e8c6bb6d68417340e0004ca") }}},false,true);

or

db.nodes.update({ "_id" : { $exists : true } },{$pull : {"parents": { "id" : ObjectId("4e8c6bb6d68417340e0004ca") }}},false,true);

i haven't tested the code myself, but am sure that any one of the above will work..

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