0

I have a mongodb collection in the format:

    {
    _id:"xxxx"
    comments : [
            {
                comment:"I know good art is supposed to be controversial"
                commentid:"820168"
                username:"bleckfella"
                isprocessed:0
            },
            {
                comment:"Next thing.."
                commentid:"820846"
                username:"egzegs"
                isprocessed:0
            },
            {
                comment:"Good decision, please stay away Jamie."
                commentid:"820901"
                username:"Capt_mulch"
                isprocessed:1
            }
        ]

    some fields.....
    ....
    ....
}
{
    _id:"xxxx"
    comments : [
            {
                comment:"who REALLY cares?"
                commentid:"820162"
                username:"BigBunny"
                isprocessed:0
            },
            {
                comment:"Double oh dear ..."
                commentid:"820849"
                username:"MrX"
                isprocessed:1
            },
            {
                comment:"Good decision."
                commentid:"830501"
                username:"mark11"
                isprocessed:0
            }
        ]

    some fields.....
    ....
    ....
}
{
        _id:"xxxx"
    comments : [
            {
                comment:"my condolences Mick. My thoughts are with you."
                commentid:"821164"
                username:"BigBunny"
                isprocessed:0
            },
            {
                comment:"Three letters.     O. M. G."
                commentid:"840844"
                username:"MrX"
                isprocessed:0
            }

        ]

    some fields.....
    ....
    ....
}

Now I want to query this data for comments having value of "isprocessed" field as "1" The queries I have tried from shell are :

db.coll.find({comments:{$elemMatch: {isProcessed:1}}},{"comments.isProcessed":1,"comments.comment":1}).pretty()

&

db.coll.find({"comments.isProcessed":1},{"comments.isProcessed":1,"comments.comment":1}).pretty()

Both are giving the same output which is:

{
    _id : "xxxx"
    "comments : [
            {
                comment:"I know good art is supposed to be controversial"
                isprocessed:0
            },
            {
                comment:"Next thing.."
                isprocessed:0
            },
            {
                comment:"Good decision, please stay away Jamie."
                isprocessed:1
            }
        ]

}
{
    _id : "xxxx"
    comments : [
            {
                comment:"who REALLY cares?"
                isprocessed:0
            },
            {
                comment:"Double oh dear ..."
                isprocessed:1
            },
            {
                comment:"Good decision."
                isprocessed:0
            }
        ]

}

Means it is showing the comments with "isprocessed" field value "0" for matched document which I dont need. I need the output as :

{
    "_id":"xxxx"
    comments : [
            {
                comment:"Good decision, please stay away Jamie."
                isprocessed:1
            }
        ]
}
{
    _id:"xxxx"
    comments : [
            {
                comment:"Double oh dear ..."
                isprocessed:1
            }
        ]
}

I also need to update the value of field "isprocessed" after reading it in this format. How to write a query for that. Plz suggest any solution.

user2710961
  • 69
  • 2
  • 15

1 Answers1

0

try following command

db.coll.aggregate([{$unwind: '$comments'}, {$match : {'comments.isprocessed' : 1}}, {$project : {_id : 1, 'comments.comment' : 1, 'comments.isprocessed' : 1}}])
dikesh
  • 2,977
  • 2
  • 16
  • 26