0

I want to select top 3 row from all individual user_obj (i.e 1,2,3) and then finally group them and sort with maximum number of count.

Document Sample

{
    "_id" : ObjectId("5936986f6b5f90330f8e34ba"),
    "factual_id" : "7b758ba0-1e18-012f-0643-0030487f54d8",
    "user_obj" : 1,
    "name" : "Buckhorn Bar",
    "address" : "105 S MAIN St",
    "address_extended" : null,
    "po_box" : "P.O. Box 115",
    "locality" : "Viborg",
    "region" : "SD",
    "post_town" : null,
    "admin_region" : null,
    "postcode" : NumberLong("57070"),
    "country" : "us",
    "tel" : "(605) 766-0086",
    "fax" : null,
    "latitude" : 43.170177,
    "longitude" : -97.081538,
    "neighborhood" : null,
    "website" : null,
    "email" : null,
    "category_ids" : [
        NumberLong("347"),
        NumberLong("312")
    ],
    "category_labels" : [
        [
            "Social",
            "Food and Dining",
            "Restaurants"
        ],
        [
            "Social",
            "Bars"
        ]
    ],
    "chain_name" : null,
    "chain_id" : null,
    "hours" : null,
    "hours_display" : null,
    "existence" : 0.7,
    "cuisine" : [
        "Pub Food"
    ]
},

/* 2 createdAt:6/6/2017, 5:26:31 PM*/
{
    "_id" : ObjectId("5936986f6b5f90330f8e34bb"),
    "factual_id" : "7b8a33b9-9ca9-4068-b711-012a77d016a4",
    "user_obj" : 3,
    "name" : "Retro Bean",
    "address" : "10606 Sales Rd S",
    "address_extended" : null,
    "po_box" : null,
    "locality" : "Tacoma",
    "region" : "WA",
    "post_town" : null,
    "admin_region" : null,
    "postcode" : NumberLong("98444"),
    "country" : "us",
    "tel" : "(253) 267-0272",
    "fax" : null,
    "latitude" : 47.160925,
    "longitude" : -122.46548,
    "neighborhood" : null,
    "website" : null,
    "email" : null,
    "category_ids" : [
        NumberLong("342")
    ],
    "category_labels" : [
        [
            "Social",
            "Food and Dining",
            "Cafes, Coffee and Tea Houses"
        ]
    ],
    "cuisine" : [
        "Tea"
    ],
}

.... more document here

At present this what I'm doing

db.restaurant.find([
    {
        $match: {
            user_obj:{
                $in:[1,2,3]

            },
            cuisine:{$ne: ["",null]}
        }

    },
    {$unwind: "$cuisine"}, 
    {
        $group:{
            _id:"$cuisine",count:{$sum:1}    
        }
    },
    {$sort:{count:-1}}, 
    {$limit:3}
])

So the query should find top 3 cuisine for the user_obj 1 then top 3 cuisine for user_obj 2 and then user_obj 3

the above query work well with one user_obj at a time ,but this has to be done with multiple user_obj. As I'm new to this technology so I need help to modify this query to get the response with multiple id

Aman Maurya
  • 1,305
  • 12
  • 26

0 Answers0