MongoDB:使用聚合为每个条件选择前N行

时间:2017-06-22 15:21:41

标签: mongodb

我想从所有个体user_obj(即1,2,3)中选择前3行,然后最后对它们进行分组并按最大计数进行排序。

文件样本

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

....更多文档在这里

目前我正在做的事情

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}
])

因此,查询应该为user_obj 1找到前3种菜肴,然后为user_obj 2找到前3种菜肴然后user_obj 3

上述查询一次只能使用一个user_obj,但必须使用多个user_obj。由于我是这项技术的新手,所以我需要帮助来修改此查询以获得具有多个ID的响应

0 个答案:

没有答案
相关问题