[英]Querying MongoDB collection having array of JSON documents
I have a mongodb collection in the format: 我有以下格式的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.....
....
....
}
Now I want to query this data for comments having value of " isprocessed " field as " 1 " The queries I have tried from shell are : 现在,我要查询此数据以获取值为“ 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()
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. 表示它为我不需要的匹配文档显示带有“ isprocessed ”字段值“ 0 ”的注释。 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. 在以这种格式读取字段之后,我还需要更新字段“ isprocessed ”的值。 How to write a query for that.
如何为此编写查询。 Plz suggest any solution.
请提出任何解决方案。
尝试以下命令
db.coll.aggregate([{$unwind: '$comments'}, {$match : {'comments.isprocessed' : 1}}, {$project : {_id : 1, 'comments.comment' : 1, 'comments.isprocessed' : 1}}])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.