3

MongoDB を使用しており、基準に一致する DB 内のドキュメントから配列要素 (それ自体が埋め込まれたドキュメント) を削除しようとしています。このために、更新コマンドで $pull 演算子を使用しようとしています。しかし、場合によってはこれを機能させることができません (以下の説明を参照)。私は何が欠けていますか?

前もって感謝します。

-さちん

> use test
switched to db test

//First, insert a record with an array of addresses, with array elements being embedded objects with exactly 1 element (email)
> db.users.insert({
    name: 'smith',
    addresses:[{email:'a@b'},{email:'c@d'}]
    });... ... ...


//Result of the insertion
> db.users.find()
{ "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ { "email" : "a@b" }, { "email" : "c@d" } ] }


//From records with name= Smith, try to $pull any array elements with email a@b

> db.users.update({name:'smith'}, {$pull:{addresses:{email:'a@b'}}});> 

//After successful $pull

> db.users.find()
{ "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ { "email" : "c@d" } ] }


//Now insert  a record with an array of addresses, with array elements being embedded objects with exactly 2 elements (email, phone)

> db.users.insert({
    name: 'smith',
    addresses:[{email:'a@b', phone: '12345'},{email:'c@d',phone :'54321'}]
    });... ... ... 


//Result of the insertion

> db.users.find()
{ "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ { "email" : "c@d" } ] }
{ "_id" : ObjectId("50226bfc545b516cdbadbcda"), "name" : "smith", "addresses" : [
        {
                "email" : "a@b",
                "phone" : "12345"
        },
        {
                "email" : "c@d",
                "phone" : "54321"
        }
] }

//From records with name= Smith, again try to $pull any array elements with email a@b

> db.users.update({name:'smith'}, {$pull:{addresses:{email:'a@b'}}})


// - Unsuccessful $pull (Why? How to fix this)
> db.users.find()
{ "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ { "email" : "c@d" } ] }
{ "_id" : ObjectId("50226bfc545b516cdbadbcda"), "name" : "smith", "addresses" : [
        {
                "email" : "a@b",
                "phone" : "12345"
        },
        {
                "email" : "c@d",
                "phone" : "54321"
        }
] }


//Meanwhile, the single element pull still works as before -

> db.users.update({name:'smith'}, {$pull:{addresses:{email:'c@d'}}})


> db.users.find()
{ "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ ] }
{ "_id" : ObjectId("50226bfc545b516cdbadbcda"), "name" : "smith", "addresses" : [
        {
                "email" : "a@b",
                "phone" : "12345"
        },
        {
                "email" : "c@d",
                "phone" : "54321"
        }
] }
> 

うまくいきませんでしたが、レスポセンに感謝します。Mongo シェルのトランスクリプトを次に示します。

> db.users.find()
{ "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ ] }
{ "_id" : ObjectId("50226bfc545b516cdbadbcda"), "name" : "smith", "addresses" : [
        {                               
                "email" : "a@b",
                "phone" : "12345"
        },
        {
                "email" : "c@d",
                "phone" : "54321"
        }
] }
> db.users.update({name:'smith'}, {$pull:{"addresses.email": 'a@b'}})
Modifier spec implies existence of an encapsulating object with a name that already represents a non-object, or is referenced in another $set clause
> db.users.find()
{ "_id" : ObjectId("50226b46545b516cdbadbcd9"), "name" : "smith", "addresses" : [ ] }
{ "_id" : ObjectId("50226bfc545b516cdbadbcda"), "name" : "smith", "addresses" : [
        {
                "email" : "a@b",
                "phone" : "12345"
        },
        {
                "email" : "c@d",
                "phone" : "54321"
        }
] }
> 

...基本的にドット表記はうまくいきませんでした。

4

1 に答える 1

2

わかりましたので、答えを見つけました。

まず、MongoDB 1.2.2 を使用していましたが、このバージョンは上記の $pull 操作の更新をサポートしていませんでした。

次に、MongoDB 2.06 (最新の安定版) にアップグレードしました。次に、1.2.2 で作成した古いデータベースを使用すると、同じ結果になります。

次に、2.06 で新しい DB を作成し、@sergio-tulentsev による提案、つまり db.users.update({name:'smith'}, {$pull:{"addresses.email": 'a@b '}})

残念ながら、これもうまくいきませんでした。

最後に、実行できなかった初期コマンドを試してみました

db.users.update({name:'smith'}, {$pull:{addresses:{email:'a@b'}}})

そしてそれは働いた!!!

要点: 1. MongoDB サーバーを更新する 2. 古いバージョンのデータベース ファイルは機能せず、新しいデータベース ファイルでのみ機能します。今、どうにかしてデータを新しいバージョンに移行する必要があります。

更新:...この移行は、mongod --upgrade コマンドを発行するのと同じくらい簡単でした。

于 2012-08-09T08:57:57.840 に答える