0

This is my mongodb document.

"_id" : ObjectId("5123731f2763c9682a000001"),
"created_on" : ISODate("2013-02-19T12:42:07.835Z"),
"currentLogin" : ISODate("2013-02-22T06:03:25.603Z"),
"email" : "[email protected]",
"extraPhotos" : 0,
"reqmail" : [ ],
"isActivated" : true,
"lastLogin" : ISODate("2013-02-20T05:42:15.359Z"),
"linkedAccounts" : [
    {
        "birthday" : "11/01/1984",
        "email" : "[email protected]",
        "expiresIn" : "5184000",
        "expiryMailNotifFlag" : -1,
        "extraPhotos" : 0,
        "fetchFromDate" : null,
        "fetchOrder" : "oldest",
        "fetchUntilDate" : "04/11/2014",
        "locale" : "en_US",
        "optIn" : true,
        "timezone" : "5.5",
        "userID" : "100000207309657"
    }
],
"maxLinkedAccounts" : 4,
"name" : "venu wale",
"notifications" : [
    {
        "_id" : ObjectId("51275fb7389b85f222000001"),
        "type" : "limitUsed",
        "message" : "Account limit is used",
        "accID" : "100000207309657",
        "date" : ISODate("2013-02-22T12:08:23.419Z")
    },
    {
        "_id" : ObjectId("51275fe8389b85f222000002"),
        "type" : "limitUsed",
        "message" : "Account limit is used",
        "accID" : "100000207309657",
        "date" : ISODate("2013-02-22T12:09:12.385Z")
    },
    {
        "_id" : ObjectId("51276020389b85f222000003"),
        "type" : "limitUsed",
        "message" : "Account limit is used",
        "accID" : "100000207309657",
        "date" : ISODate("2013-02-22T12:10:08.275Z")
    },
    {
        "_id" : ObjectId("5127605d389b85f222000004"),
        "type" : "limitUsed",
        "message" : "Account limit is used",
        "accID" : "100000207309657",
        "date" : ISODate("2013-02-22T12:11:09.946Z")
    },
    {
        "_id" : ObjectId("51276097389b85f222000005"),
        "type" : "limitUsed",
        "message" : "Account limit is used",
        "accID" : "100000207309657",
        "date" : ISODate("2013-02-22T12:12:07.384Z")
    },
    {
        "_id" : ObjectId("512760d2389b85f222000006"),
        "type" : "limitUsed",
        "message" : "Account limit is used",
        "accID" : "100000207309657",
        "date" : ISODate("2013-02-22T12:13:06.933Z")
    }
],
"password" : "638cfc167e8431c01227e4f113ec9427821b12493ed6fd3",
"phone" : "5555555555",
"photo_updated_time" : 1361341819,
"photosProcessed" : 15,
"photosToProcess" : 400,
"plan" : {
    "type" : "photoOnly"
},
"salt" : "af8ab2f5866642f03d31aa3f4a24e25e287bedc7",
"updated_on" : ISODate("2013-02-19T12:42:07.835Z")

I Want to delete any one object from notifications array. I am using this query.

db.users.update({phone:'5555555555'},{$unset:{notifications:{$elemMatch:{_id:ObjectId('51276020389b85f222000003')}}}})

But its not working.

1 Answer 1

4

You only need to pull this element from the notifications array:

db.collection.update({phone:"5555555555"},{$pull:{notifications: {_id:ObjectId("51276020389b85f222000003")}}})

Using pull, you can remove all elements that match your inner query. The next $pull

{$pull:{ notifications: {type: "limitUsed"}}}

will remove all notifications of type "limitUsed".

Sign up to request clarification or add additional context in comments.

1 Comment

Isn't MongoDB awesome? :P but what if I wanted the inner query to exactly match the whole array item?

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.