查询具有JSON文档数组的MongoDB集合

时间:2014-04-03 12:16:32

标签: arrays mongodb

我有一个mongodb集合,格式为:

    {
    _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.....
    ....
    ....
}

现在,我想查询此数据,查看值为“ isprocessed ”字段的评论为“ 1 ” 我从shell尝试的查询是:

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()

两者都提供相同的输出:

{
    _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
            }
        ]

}

表示对于我不需要的匹配文档显示带有“ isprocessed ”字段值“ 0 ”的评论。 我需要输出为:

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

在以此格式读取字段后,我还需要更新字段“ isprocessed ”的值。 如何为此编写查询。 Plz建议任何解决方案。

1 个答案:

答案 0 :(得分:0)

尝试以下命令

db.coll.aggregate([{$unwind: '$comments'}, {$match : {'comments.isprocessed' : 1}}, {$project : {_id : 1, 'comments.comment' : 1, 'comments.isprocessed' : 1}}])