2017-11-05 7 views
2

私はMongo DBリクエストに助けが必要です。mongodbの特定のオブジェクトで単一の配列要素を削除するには?

これは私の文書構造である:

{ 
"username" : "firstUser", 
"email" : "[email protected]", 
"subscriptions" : [ 
    { 
     "subscriptionId" : ObjectId("59f972dfdaca9e39487e3bb4"), 
     "someOtherFields" : "otherValue", 
     "message" : { 
      "contact" : [ 
       "[email protected]", 
       "[email protected]", 
       "[email protected]" 
      ], 
      "subject" : "Mailsubject", 
      "content" : "Mailcontent" 
      } 
    }, 
    { 
     "subscriptionId" : ObjectId("59faf26c8a593b25b8a9a8f7"), 
     "someOtherFields" : "otherValue", 
     "message" : { 
      "contact" : [ 
       "[email protected]", 
       "[email protected]", 
       "[email protected]" 
      ], 
      "subject" : "Mailsubject", 
      "content" : "Mailcontent" 
      } 
    } 
} 

今私はsubscriptions.subscriptionid = ObjectId("59f972dfdaca9e39487e3bb4")subscriptions.message.contact配列から1つのE-mailアドレスを削除する必要があります。

は、私はこのような何か試してみました:

db.getCollection('myCollection').update({ 
"subscriptions.subscriptionId" : ObjectId("59f972dfdaca9e39487e3bb4") 
}, 
{ 
    "$pull" : { "subscriptions": {"message.contact" : "[email protected]" }} 
}) 

をしかしそれは、このE-mailアドレスを持つすべてのsubscriptionsを削除

は誰もが、どのようにこの問題を解決するために知っていますか?

答えて

3

これを使用できます。

配列の要素を識別するために、$演算子を使用しました。

db.getCollection('myCollection').update({ 
"subscriptions.subscriptionId" : ObjectId("59f972dfdaca9e39487e3bb4") 
}, 
{ 
    "$pull" : {"subscriptions.$.message.contact" :"[email protected]"} 
} 
) 
+0

多くの感謝、それは私が必要としていたものです。 –

+0

よろしくお願いします! –

関連する問題