正常的sql语句为
select financing ,count(financing) ,avg(live_days)/30
from company
GROUP BY financing
正常的mongodb语句为
db.getCollection("data").aggregate([
{$group:{_id:"$financing",counter:{$sum:1},num_tutorial:{$avg:'$live_days'}}}
])
但是其中live_days为String类型字段,不能进行avg,所以只能单拿出来用java语句单独求平均值
//连接数据库
MongoClient mongoClient = new MongoClient( "localhost", 27017);
MongoDatabase databasee = mongoClient.getDatabase("temp");
MongoCollection<Document> collection = databasee.getCollection("data");
List<Back_data> list= new ArrayList<Back_data>();
//mongodb聚合函数,相当于sql中的select financing ,count(financing) from company GROUP BY financing
Document info_group = new Document();
info_group.put("_id", "$financing");
info_group.put("coun