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"
}
] }
>
...基本的にドット表記はうまくいきませんでした。