如何根据 MongoDB 中的列表计数过滤文档? [复制]

Posted

技术标签:

【中文标题】如何根据 MongoDB 中的列表计数过滤文档? [复制]【英文标题】:How do I filter documents based on the count of a list in MongoDB? [duplicate] 【发布时间】:2019-08-15 23:57:18 【问题描述】:

在 MongoDB 中,我正在寻找包含至少 5 条餐厅评论的文档。 我尝试在 $match 上添加过滤器,例如:

"grades.score: $gt: 4

但是,这会给我提供评论评分至少为 5 或更高的文档,而我想要的是评论计数至少为 5。

db.restaurants.aggregate([
...         "$match":
...             "$and":[
...                 "borough":"Bronx",
...                  "cuisine": "American ",
...                 "address.zipcode":"10467"],
...     "$group":
...         "_id":
...             "name" : "$name",
...              "grades.score" : "$grades.score"])

这是我得到的:

 "_id" :  "name" : "Zymi Bar & Grill", "grades.score" : [ 75, 5, 18 ]  
 "_id" :  "name" : "Allerton Diner", "grades.score" : [ 20, 9 ]  
 "_id" :  "name" : "Gasolina Bar Lounge", "grades.score" : [ 10, 10 ]  
 "_id" :  "name" : "Salud Y Estilo De Vida", "grades.score" : [ 0, 7, 7, 6 ]  
 "_id" :  "name" : "Coffee Shop", "grades.score" : [ 20, 12, 12, 9, 2 ]  
 "_id" :  "name" : "Nicky'S Coffee Shop", "grades.score" : [ 7, 9, 12, 13, 3 ]  
 "_id" :  "name" : "John'S Luncheonette", "grades.score" : [ 5, 5, 8, 12, 11 ]  
 "_id" :  "name" : "Kennedy'S Chicken & Pizza", "grades.score" : [ 7 ]  
 "_id" :  "name" : "V.I.P.'S Cafe", "grades.score" : [ 9, 11, 17, 7, 23, 9 ]  
 "_id" :  "name" : "Woodlawn Cafe", "grades.score" : [ 5, 13, 12, 11, 27, 2 ]  
 "_id" :  "name" : "Moshulo Golf Course", "grades.score" : [ 11 ]  
 "_id" :  "name" : "Kennedy Fried Chicken", "grades.score" : [ 4, 9, 5, 9 ]  
 "_id" :  "name" : "Kennedy'S Chicken And Pizza", "grades.score" : [ 9, 19, 13, 12, 26 ]  
 "_id" :  "name" : "502 Bar Lounge", "grades.score" : [ 4, 4, 2, 0 ]  
 "_id" :  "name" : "Burger Barn Restaurant", "grades.score" : [ 8, 9, 11, 16, 19 ]  

这是我需要的:

 "_id" :  "name" : "Coffee Shop", "grades.score" : [ 20, 12, 12, 9, 2 ]  
 "_id" :  "name" : "Nicky'S Coffee Shop", "grades.score" : [ 7, 9, 12, 13, 3 ]  
 "_id" :  "name" : "John'S Luncheonette", "grades.score" : [ 5, 5, 8, 12, 11 ]  
 "_id" :  "name" : "V.I.P.'S Cafe", "grades.score" : [ 9, 11, 17, 7, 23, 9 ]  
 "_id" :  "name" : "Woodlawn Cafe", "grades.score" : [ 5, 13, 12, 11, 27, 2 ]  
 "_id" :  "name" : "Kennedy'S Chicken And Pizza", "grades.score" : [ 9, 19, 13, 12, 26 ]  
 "_id" :  "name" : "Burger Barn Restaurant", "grades.score" : [ 8, 9, 11, 16, 19 ]  

【问题讨论】:

您能展示一下您的原始文档是什么样子的吗 find( borough":"Bronx", "cuisine": "American", "address.zipcode":"10467", "grades.score.4": "$exists": true , "name": 1, "grades.score": 1 )。绝对不需要聚合语句。如果您需要做其他任何事情,最多只是一个$match 和一个$project 【参考方案1】:

你可以根据你的例子试试这个:

db.restaurants.aggregate([
        "$match": 
            "$and": [
                    "borough": "Bronx"
                ,
                
                    "cuisine": "American "
                ,
                
                    "addresszipcode": "10467"
                
            ]
        
    ,
    
        "$group": 
            "_id": 
                "name": "$name",
                "score": "$grades.score",
            
        
    ,
    
        $match: 
            $expr: 
                $gte: [
                    $size: "$score"
                , 5]
            
        
    
])

使用最终的 $match 和 $expr 来执行聚合表达式

【讨论】:

【参考方案2】:

非常感谢朱利安。

最后我改用 $project 解决了它。很抱歉在找到解决方案之前没有回复。

pipeline1 = []
step1 = "$match":"$and":["borough": borough, "cuisine": cuisine,"address.zipcode": zipcode]
pipeline1.append(step1)
step2 = "$project":  "_id":0, "name":1, "fiveOrMore":  "$gte": [ "$size":"$grades.score" , 5 ] , "grades.score" :1 
pipeline1.append(step2)
step3 = "$match":  "fiveOrMore" : True 
pipeline1.append(step3)
step4 = "$unwind": "$grades"
pipeline1.append(step4)
step5 = "$group":"_id": "$name", "averageReview": "$avg": "$grades.score"
pipeline1.append(step5)

【讨论】:

以上是关于如何根据 MongoDB 中的列表计数过滤文档? [复制]的主要内容,如果未能解决你的问题,请参考以下文章

如何根据MongoDB中文档字段中的每个数组项过滤集合

根据 MongoDB 中的字典键过滤文档

如何根据 id 加载多个文档?

使用 MongoDB 中的文档属性过滤器获取嵌入数组中的扁平文档数组

如何匹配 MongoDB 中的子文档数组?

如何使用MongoDB过滤子文档中的数组[重复]