mongodb2.6没有返回最小值

时间:2016-01-22 05:23:18

标签: mongodb aggregation-framework

我在mongodb中有一个集合,我想运行一个查询,为每个servicecost返回最小serviceType。相关领域是:

> db.vendor_master.find({},{"serviceType":1,"servicecost":1,"_id":0})
{ "serviceType" : "Florist", "servicecost" : 10000 }
{ "serviceType" : "Florist", "servicecost" : 10000 }
{ "serviceType" : "DJ", "servicecost" : 10000 }
{ "serviceType" : "Florist", "servicecost" : 10000 }
{ "serviceType" : "Florist", "servicecost" : 10000 }
{ "serviceType" : "Decorator", "servicecost" : 10000 }
{ "serviceType" : "Decorator", "servicecost" : 10000 }
{ "serviceType" : "Decorator", "servicecost" : 12000 }
{ "serviceType" : "Pandit", "servicecost" : 3500 }
{ "serviceType" : "Pandit", "servicecost" : 1000 }
{ "serviceType" : "Pandit", "servicecost" : 5000 }
{ "serviceType" : "Pandit", "servicecost" : 1000 }
{ "serviceType" : "Florist", "servicecost" : 10000 }
{ "serviceType" : "DJ", "servicecost" : 10000 }
{ "serviceType" : "DJ", "servicecost" : 10000 }
{ "serviceType" : "DJ", "servicecost" : 10000 }
{ "serviceType" : "DJ", "servicecost" : 10000 }
{ "serviceType" : "Band", "servicecost" : 1000 }
{ "serviceType" : "Band", "servicecost" : 50000 }
{ "serviceType" : "Band", "servicecost" : 152500 }
{ "serviceType" : "Decorator", "servicecost" : 2000 }
{ "serviceType" : "Decorator", "servicecost" : 25000 }
{ "serviceType" : "Pandit", "servicecost" : 1000 }
{ "serviceType" : "Band", "servicecost" : 12500 }
{ "serviceType" : "Band", "servicecost" : 15000 }

我正在尝试以下查询

db.vendor_master.aggregate([{$group:{_id:"serviceType",mincost:{$min:"servicecost"}}}])

输出是:

{ "_id" : "serviceType", "mincost" : "servicecost" }

预期输出应为每个serviceType,且servicecost最小。

知道我在哪里错过了吗?

由于

1 个答案:

答案 0 :(得分:0)

$min将一个表达式作为其操作数。

{ $min: <expression> }

表达式必须以documentation中提到的美元符号$作为前缀:

  

聚合表达式使用field path来访问输入文档中的字段。要指定字段路径,请使用前缀为美元符号$字段名称或带点字段名称的字符串(如果该字段位于嵌入文档中)。

db.vendor_master.aggregate([
    { "$group": {
        "_id": "serviceType",
        "mincost": { "$min": "$servicecost" }
    }}
])