Mongoldb聚合检查数组中的字段值出现了多少次?
我有一个文档集合,看起来像这个
{
_id : 21353456,
product : "xy",
text : "asdf",
reviews : [
{
username : "User1",
userID: 12
text : "hi",
},
{
username : "User2",
userID: 123
text : "hi1",
}
]
}
用户可以对不同的产品进行多次评论,我想检索所有至少进行了 3 次评论的用户。我想查看评论数量、评论者姓名和 ID(按字母顺序排列)。 我已经尝试过这段代码,但它不起作用
db.reviews.aggregate([{
$group:{
"_id": "$userID", "$userName","$text"
"numRev":{$numRev:{}}}}, {$match:{"numRev":{$gte: 3}}}, {$sort: {"reviewerName" : 1}}])
I have a collection of documents that look like this
{
_id : 21353456,
product : "xy",
text : "asdf",
reviews : [
{
username : "User1",
userID: 12
text : "hi",
},
{
username : "User2",
userID: 123
text : "hi1",
}
]
}
users can make multiple reviews on different products, I want to retrieve all the users that have made at least 3 reviews. I want to see the number of reviews, the reviewer name and id in alphabetic order.
I have tried this code but it doesn't work
db.reviews.aggregate([{
$group:{
"_id": "$userID", "$userName","$text"
"numRev":{$numRev:{}}}}, {$match:{"numRev":{$gte: 3}}}, {$sort: {"reviewerName" : 1}}])
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
也许是这样的:
解释:
游乐场
Maybe something like this:
Explained:
playground