简体   繁体   English

您如何在Mongo中计算最大和唯一性?

[英]How do you compute max and unique in Mongo?

I have a bunch of documents: 我有一堆文件:

{ "name" : "A", "value" : 10},
{ "name" : "B", "value" : 12},
{ "name" : "B", "value" : 14},
{ "name" : "A", "value" : 16},
{ "name" : "C", "value" : 11}

I want to find the minimum value for each name, so the result would be 我想找到每个名称的最小值,所以结果是

{ "name" : "A", "value" : 10},
{ "name" : "B", "value" : 12},
{ "name" : "C", "value" : 11}

What is the correct way to do this query in Mongo? 在Mongo中执行此查询的正确方法是什么? I keep thinking in terms of SQL. 我一直在思考SQL。

For the minimum, you can do this by using Mongo's Aggregation Framework with " $group " and "$min". 至少,您可以通过将Mongo的Aggregation Framework与“ $ group ”和“ $ min”结合使用。 Assuming that you had a collection named "myCollection," you could try something like this: 假设您有一个名为“ myCollection”的集合,则可以尝试如下操作:

db.myCollection.aggregate({$group:{_id:"$name", minimumValue: {$min : "$value"}}});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM