1

i need some help with my Mongo DB request.

This is my document structure:

{
 "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"
          }
     }
}

Now i need to remove one E-Mail address from the subscriptions.message.contact array, where the subscriptions.subscriptionid = ObjectId("59f972dfdaca9e39487e3bb4").

I tried something like this:

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

but it removed all subscriptions with this E-Mail address

Does anyone know, how to solve this problem?

0

1 Answer 1

3

You can use this.

I used $ operator for identifying the elements in the subscriptions array.

db.getCollection('myCollection').update({
"subscriptions.subscriptionId" : ObjectId("59f972dfdaca9e39487e3bb4")
},
{
    "$pull" : {"subscriptions.$.message.contact" :"[email protected]"}
}
)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.