0

I am extremely new to mongodb and am facing a little trouble with an update operation. Here is the document:

{
    "username" : "amitverma",
    "notifications" : {
        "notification_add_friend" : [
            {
                "sender" : "macbook",
                "action" : "",
                "type" : "",
                "objectType" : "request",
                "objectUrl" : "",
                "isUnread" : true
            },
            {
                "sender" : "safari",
                "action" : "",
                "type" : "",
                "objectType" : "request",
                "objectUrl" : "",
                "isUnread" : true
            },
            {
                "sender" : "chrome",
                "action" : "",
                "type" : "",
                "objectType" : "request",
                "objectUrl" : "",
                "isUnread" : true
            }
        ]
    },
    "_id" : ObjectId("526598c86f45240000000001")
}
{
    "username" : "macbook",
    "notifications" : {
        "notification_add_friend" : [
            {
                "sender" : "amitverma",
                "action" : "",
                "type" : "",
                "objectType" : "a_r",
                "objectUrl" : "",
                "isUnread" : true
            }
        ]
    },
    "_id" : ObjectId("526598d06f45240000000002")
}

I want to remove the sub array with {"sender":"safari"} within "username":"amitverma" I have tried $elemMatch with $set, but just couldn't get the query correctly.

2
  • If I understand you correctly, you want to delete the whole notification_add_friend array from the first document? P.S. Actually there are two documents here Commented Oct 21, 2013 at 22:01
  • I think the OP means to remove the "safari" request from the array but is wording it a bit strangely. If the intention is to remove the whole array I'll delete/change my answer. Commented Oct 21, 2013 at 22:07

1 Answer 1

3

You don't want to use $set here, but $pull (see docs), and while you could use $elemMatch to further specify your query, you do not need to.

The following would pull all add friend notifications with {"sender": "safari"} from the sub array of documents matching {"username": "amitverma"}

db.yourcollection.update({"username": "amitverma"}, { 
  $pull: {"notifications.notifications_add_friend": {"sender": "safari"}}
})

As to your comment, if you wanted to update a particular element you would use $set in combination with $elemMatch and the positional operator $. For your example, something like:

db.yourcollection.update({
  "username": "amitverma", 
  "notifications.notifications_add_friend": {
    $elemMatch: {"sender": "safari"}
  }
}, {
  $set: {
    "notifications.notifications_add_friend.$.isUnread": false
  }
})
Sign up to request clarification or add additional context in comments.

3 Comments

what if i want to change a particular property, say, isUnread to false.. how would I do that?
the update solution for changing the value of isUnread doesnt work.
It should work, are you getting an error or is it just not updating? And what version of mongo are you on?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.