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 ] } }
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$matchand a$projectif you needed to do anything else.