版权声明:不短不长八字刚好@wzy https://blog.csdn.net/qq_38089964/article/details/83929070
部分需求需要在mongodb的集合中按照关键字分组并统计出个数,如:一个集合里是所有公司的信息,然后在前端页面中需要展示公司的分布情况,这时就需要后端对公司所在的城市分组求和,然后给出每个城市中公司的数量。这个操作就需要mongodb的聚合函数了(aggregate);
举个例子,集合中有公司的city关键字,我们需要对city分组,然后求和。
聚合命令:
db.getCollection('company_profile').aggregate(
[{ "$group" : {
"_id" : "$city",
"value" : { "$sum" : 1 }
}
}]
)
显然,这个格式并不是很好用,因为数据需要弄到echars的地图上,需要换成name:成都市,value:50的形式。
换格式,在后面加条命令:{ "$project" : { "_id" : 0, "name" : "$_id", "value" : "$value" } }
意思就是说:name字段的值就是上面查询出来的_id,value就是上面的value,_id则不需要。
命令如下:
db.getCollection('company_profile').aggregate(
[{ "$group" : {
"_id" : "$city",
"value" : { "$sum" : 1 }
}
},
{ "$project" : {
"_id" : 0,
"name" : "$_id",
"value" : "$value"
}
}]
)
查询结果:
扫描二维码关注公众号,回复:
4874299 查看本文章
java代码:
package org.kelab.enterprise.dao;
import com.mongodb.*;
import com.mongodb.client.AggregateIterable;
import com.mongodb.client.MongoCursor;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;
import org.bson.conversions.Bson;
import org.cn.wzy.util.PropertiesUtil;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
MongoClientOptions options = MongoClientOptions.builder()
.connectionsPerHost(PropertiesUtil.IntegerValue("mongo.connectionsPerHost"))
.maxWaitTime(PropertiesUtil.IntegerValue("mongo.maxWaitTime"))
.socketTimeout(PropertiesUtil.IntegerValue("mongo.socketTimeout"))
.maxConnectionLifeTime(PropertiesUtil.IntegerValue("mongo.maxConnectionLifeTime"))
.connectTimeout(PropertiesUtil.IntegerValue("mongo.connectTimeout"))
.serverSelectionTimeout(PropertiesUtil.IntegerValue("mongo.serverSelectionTimeout"))
.localThreshold(PropertiesUtil.IntegerValue("mongo.localThreshold"))
.build();
ServerAddress serverAddress = new ServerAddress(PropertiesUtil.StringValue("mongo.host"),
PropertiesUtil.IntegerValue("mongo.port"));
List<ServerAddress> addrs = new ArrayList<>();
addrs.add(serverAddress);
MongoCredential credential = MongoCredential.createScramSha1Credential(
PropertiesUtil.StringValue("mongo.user")
, PropertiesUtil.StringValue("mongo.connect")
, PropertiesUtil.StringValue("mongo.pwd").toCharArray());
MongoClient mongoClient = new MongoClient(addrs, credential, options);
MongoDatabase mongo = mongoClient.getDatabase(PropertiesUtil.StringValue("mongo.db"));
List<Bson> list = new ArrayList<>();
BasicDBObject _id = new BasicDBObject("_id", "$city");
_id.append("value", new BasicDBObject("$sum", 1));
BasicDBObject group = new BasicDBObject("$group", _id);
list.add(group);
BasicDBObject result = new BasicDBObject();
result.append("_id", 0);
result.append("name", "$_id");
result.append("value", "$value");
BasicDBObject project = new BasicDBObject("$project", result);
list.add(project);
System.out.println(list);
AggregateIterable<Document> iterable = mongo.getCollection("company_profile").aggregate(list);
MongoCursor<Document> set = iterable.iterator();
while (set.hasNext()) {
Document map = set.next();
System.out.println(map);
}
}
}