开发者

Why $pull operator doesn't work on MongoDB when using empty criteria?

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!


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜