【问题标题】:How to query in mongodb to get distinct record with count如何在 mongodb 中查询以获取具有计数的不同记录
【发布时间】:2017-02-21 08:06:18
【问题描述】:

我有一个名为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'
},

我可以用 mysql 轻松完成,但我在 mongodb 中知道,因为我是 mongodb 的新手

我已经尝试过这个例子,但我什么也没找到:

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

【问题讨论】:

标签: mongodb


【解决方案1】:

请尝试以下代码。您应该按美食对记录进行分组并计算它们的数量。稍后在项目管道中,您可以定义最终外观。

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

【讨论】:

  • 哦,谢谢@hasan,这就是我正在寻找的东西,但你能解释一下它是如何工作的以及我做错了什么吗?
  • 首先聚合框架正在处理流水线阶段,所以聚合函数需要一个数组作为参数。在这个数组中,您应该定义管道阶段。在您的问题中,第一阶段必须是一个小组功能,并且您做得很好。在第二阶段,您必须使用将重塑查询结果的项目功能。例如第一组阶段将返回类似的内容:{“_id”:“墨西哥”,“计数”:“2”},{“_id”:“澳大利亚”,“计数”:“3”},项目阶段是像你得到结果一样重塑它。
猜你喜欢
  • 1970-01-01
  • 2017-03-29
  • 1970-01-01
  • 2021-11-21
  • 2023-04-07
  • 2012-09-10
  • 1970-01-01
  • 1970-01-01
  • 2015-12-18
相关资源
最近更新 更多