0

In MongoDB I'm looking for documents with at least 5 restaurant reviews. I've tried adding a filter on $match, such as:

{"grades.score: {$gt: 4}}

However this will give me documents where the reviews are rated at least 5 or higher, while what I want is the review count to be at least 5.

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

This is what I get:

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

This is what I need:

{ "_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 ] } }
2
  • Could you show how your original document looks like Commented Mar 25, 2019 at 17:12
  • find({ borough":"Bronx", "cuisine": "American", "address.zipcode":"10467", "grades.score.4": { "$exists": true } },{ "name": 1, "grades.score": 1 }). Absolutely no aggregation statement required. At most, that's just a $match and a $project if you needed to do anything else. Commented Mar 26, 2019 at 7:30

2 Answers 2

1

You can try this, based from your example :

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

Usage of a final $match with $expr to perform an aggregation expression

Sign up to request clarification or add additional context in comments.

Comments

0

Thank you so much Julien.

In the end I resolved it with $project instead. Apologies for not replying before that a solution had been found.

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.