2

次のようなノードがあります。

[_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" を使用するのは問題ですか? 助けてくれてありがとう!

4

1 に答える 1

3

更新コマンドでマルチフラグを有効にしようとしましたか

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

multi - 条件に一致するすべてのドキュメントを 1 つだけではなく更新する必要があるかどうかを示します。以下の $ 演算子を使用すると便利です。

クエリを次のように変更します

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

また

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

私は自分でコードをテストしていませんが、上記のいずれかが機能すると確信しています..

于 2011-10-09T09:22:09.907 に答える