如何在mongodb中查询以获得具有count的不同记录

如何在mongodb中查询以获得具有count的不同记录,mongodb,Mongodb,我有一个名为transactions的收藏。 我正在共享事务收集的对象 { "_id" : ObjectId("58aaec83f1dc6914082afe31"), "amount" : "33.00", "coordinates" : { "lat" : "4.8168", "lon" : "36.4909" }, "cuisine" : "Mexican", "date" : ISODate("0062-02-

我有一个名为
transactions
的收藏。 我正在共享事务收集的对象

{
    "_id" : ObjectId("58aaec83f1dc6914082afe31"),
    "amount" : "33.00",
    "coordinates" : {
        "lat" : "4.8168",
        "lon" : "36.4909"
    },
    "cuisine" : "Mexican",
    "date" : ISODate("0062-02-22T11:46:52.738+05:30"),
    "location" : {
        "address" : "2414 Trudie Rue",
        "city" : "West Alisa",
        "state" : "New York",
        "zip" : "10000"
    },
    "place_name" : "Outdoors",
    "place_type" : "Wooden"
},
{
    "_id" : ObjectId("58aaec83f1dc6914082afe32"),
    "amount" : "557.00",
    "coordinates" : {
        "lat" : "-36.6784",
        "lon" : "131.3698"
    },
    "cuisine" : "Australian",
    "date" : ISODate("1294-10-04T19:53:15.562+05:30"),
    "location" : {
        "address" : "5084 Buckridge Cove",
        "city" : "Sylviaview",
        "state" : "Hawaii",
        "zip" : "51416-6918"
    },
    "place_name" : "Toys",
    "place_type" : "Cotton"
},
{
    "_id" : ObjectId("58aaec83f1dc6914082afe33"),
    "amount" : "339.00",
    "coordinates" : {
        "lat" : "45.1468",
        "lon" : "91.4097"
    },
    "cuisine" : "Mexican",
    "date" : ISODate("1568-11-25T02:54:53.046+05:30"),
    "location" : {
        "address" : "94614 Harry Island",
        "city" : "Cartwrightside",
        "state" : "Louisiana",
        "zip" : "18825"
    },
    "place_name" : "Clothing",
    "place_type" : "Frozen"
},
{
    "_id" : ObjectId("58aaec83f1dc6914082afe34"),
    "amount" : "173.00",
    "coordinates" : {
        "lat" : "-57.2738",
        "lon" : "19.6381"
    },
    "cuisine" : "Australian",
    "date" : ISODate("0804-05-07T03:00:07.724+05:30"),
    "location" : {
        "address" : "1933 Lewis Street",
        "city" : "Aufderharville",
        "state" : "Louisiana",
        "zip" : "23416"
    },
    "place_name" : "Beauty",
    "place_type" : "Fresh"
},
{
    "_id" : ObjectId("58aaec83f1dc6914082afe34"),
    "amount" : "173.00",
    "coordinates" : {
        "lat" : "-57.2738",
        "lon" : "19.6381"
    },
    "cuisine" : "Australian",
    "date" : ISODate("0804-05-07T03:00:07.724+05:30"),
    "location" : {
        "address" : "1933 Lewis Street",
        "city" : "Aufderharville",
        "state" : "Louisiana",
        "zip" : "23416"
    },
    "place_name" : "Beauty",
    "place_type" : "Fresh"
}
我想用总计数获得不同菜肴的列表

{
   "name" : 'Mexican',
   "count" : '2'
},
{
   "name" : 'Australian',
   "count" : '3'
},
输出

{
   "name" : 'Mexican',
   "count" : '2'
},
{
   "name" : 'Australian',
   "count" : '3'
},
我可以很容易地使用mysql,但我不知道mongodb,因为我是mongodb新手

我尝试过这个例子,但没有发现:

db.transactions.aggregate(
    {$group: {_id:'$cuisine'},count:{$sum:1}}
    ).result;

请尝试下面的代码。你应该把这些记录分组,并计算它们的数量。稍后在项目管道中,您可以定义最终外观

db.transactions.aggregate([
{ $group: { _id: "$cuisine", count: { $sum: 1 } } }, 
{ $project:{ _id: 0, name: "$_id", count:"$count" } }
]);

感谢@HassanAhmed的评论,可能是重复的,但我没有发现这篇文章非常有用哦,谢谢@hasan这就是我在寻找的,但你能解释一下它是如何工作的,我做错了什么吗?首先,聚合框架使用管道阶段,所以聚合函数使用数组作为参数。在此数组中,您应该定义管道阶段。在你的问题中,第一阶段必须是小组活动,你做得很好。在第二阶段中,您必须使用项目功能,该功能将重塑查询结果。例如,第一个组阶段将返回如下内容:{u id:'Mexican',“count:'2'},{u id:'Australian',“count:'3'},项目阶段正在对其进行重塑,就像您得到结果一样。