Java操作MongoDB(聚合函数)向Mongo插入及查询数据

数据库结果展示

插入后数据结构如下

在这里插入图片描述

具体内容

在这里插入图片描述

聚合函数(aggregate)

1.1 作用

用于处理数据(诸如统计平均值,求和等),并返回计算后的数据结果,是MongoDB的高级查询语言,它允许我们通过转换和合并多个文档中的数据来生成新的单个文档中不存在的信息

1.2 聚合管道

在这里插入图片描述

1.2.1 $project实例
db.article.aggregate(
    { $project : {
        title : 1 ,
        author : 1 ,
    }}
 );

这样的话结果中就只还有_id,tilte和author三个字段了,默认情况下_id字段是被包含的,如果要想不包含_id话可以这样:

db.article.aggregate(
    { $project : {
        _id : 0 ,
        title : 1 ,
        author : 1
    }});
1.2.2 $match实例
db.articles.aggregate( [
                        { $match : { score : { $gt : 70, $lte : 90 } } },
                        { $group: { _id: null, count: { $sum: 1 } } }
                       ] );

m a t c h 用 于 获 取 分 数 大 于 70 小 于 或 等 于 90 记 录 , 然 后 将 符 合 条 件 的 记 录 送 到 下 一 阶 段 match用于获取分数大于70小于或等于90记录,然后将符合条件的记录送到下一阶段 match7090group管道操作符进行处理。

1.2.3 $skip实例
db.article.aggregate(
    { $skip : 5 });

经过$skip管道操作符处理后,前五个文档被"过滤"掉

1.2.4 $group实例(按性别分组,计算年龄和)
db.user.aggregate([
    {$group:{
            _id:"$sex",
            num:{$sum:"$age"}
    }
}])
1.2.5 $limit: 限制经过管道的文档数量( $limit的参数只能是一个正整数)
db.article.aggregate({ $limit : 5 });
1.2.6 $unwind:将数组元素拆分为独立字段

例如:article文档中有一个名字为tags数组字段:

> db.article.find() {
"_id" : ObjectId("528751b0e7f3eea3d1412ce2"),
"author" : "Jone", "title" : "Abook",
"tags" : [  "good",  "fun",  "good" ] }

使用$unwind后:

> db.article.aggregate({$project:{author:1,title:1,tags:1}},{$unwind:"$tags"})
{
        "result" : [
                {
                        "_id" : ObjectId("528751b0e7f3eea3d1412ce2"),
                        "author" : "Jone",
                        "title" : "A book",
"tags" : "good"
                },
                {
                        "_id" : ObjectId("528751b0e7f3eea3d1412ce2"),
                        "author" : "Jone",
                        "title" : "A book",
"tags" : "fun"
                },
                {
                        "_id" : ObjectId("528751b0e7f3eea3d1412ce2"),
                        "author" : "Jone",
                        "title" : "A book",
  "tags" : "good"
                }
        ],
        "ok" : 1
}
1.2.7 $sort 实例
db.users.aggregate( { $sort : { age : -1, posts: 1 } });

1.3 组聚合操作符

在这里插入图片描述

1.4 Boolean类型聚合操作符

在这里插入图片描述

1.5 比较类型聚合操作符

在这里插入图片描述

1.6 算术类型聚合操作符

在这里插入图片描述

1.7 字符串类型聚合操作符

在这里插入图片描述

1.8 日期类型聚合操作符

在这里插入图片描述

1.9 条件类型聚合操作符

在这里插入图片描述
注:以上操作符都必须在管道操作符的表达式内来使用。
各个表达式使用参照:http://docs.mongodb.org/manual/reference/operator/aggregation-group/

Java操作Mongo(聚合函数)使用

2.1 插入(插入后数据库结构见最上方)

废话不多说 上代码(主要代码):

@Override
    public void addDocument(String identity, String applicationKey, List<Map<String, String>> list) {
        //创建一个连接,自带连接池效果
        MongoClient mongoClient = MongoClients.create("mongodb://192.168.10.8:27017");
        //操作一个数据库,如果数据库不存在,当您为该数据库首次存储数据时,会自动创建数据库。
        MongoDatabase spitdbDatabase = mongoClient.getDatabase(applicationKey);
        //得到一个集合对象,如果集合不存在,当您为该数据库首次存储数据时,会自动创建集合。
        MongoCollection<Document> collection = spitdbDatabase.getCollection(identity);
        //创建一个文档,参数可以接收键值对,也可以直接接收一个Map对象
        //文档对象的本质是BSON类型,该类型对应java.util.Map;BSON数组对应的是java.util.List
        List<Document> documentList = MongoUtils.listForDocument(list);
        Map<String, List<Document>> map = new HashMap<>();
        //int size = documentList.size();
        double number = 10.0;
        int n = 1;
        String[] s;
        for (Document document : documentList) {
            String gmtTarget = document.get("gmtTarget") + "";
            s = gmtTarget.split(" ");
            if (map.containsKey(s[0])) {
                if (map.get(s[0]).size() < number) {
                    map.get(s[0]).add(document);
                } else {
                    List<Document> documents = new ArrayList<>();
                    documents.add(document);
                    map.put(s[0] + "." + n, documents);
                    n++;
                }
            } else {
                List<Document> documents = new ArrayList<>();
                documents.add(document);
                map.put(s[0], documents);
            }
        }
        FindIterable<Document> documents = collection.find();
        Map<String, Object> map1 = new LinkedHashMap<>();
        int val = 0;
        for (Document document1 : documents) {
            map1.put("_id" + "." + val, document1.get("_id"));
            val++;
        }
        for (String key : map.keySet()) {
            collectionDemo(map.get(key));
            Document document = new Document();
            //当数组在表中不存在时 就新建数组
            if (documents == null || map1.containsValue(key) == false) {
                document.append("_id", key).append("gmtStart", map.get(key).get(0).get("gmtTarget")).append("gmtEnd", map.get(key).get(map.get(key).size() - 1).get("gmtTarget"))
                        .append("count", map.get(key).size()).append("records", map.get(key));
                collection.insertOne(document);
            } else {
                //若存在 则在原有对应数组后插入新数据
                Document y2 = null;
                List<Document> list2 = map.get(key);
                for (int i = 0; i < map.get(key).size(); i++) {
                    String gmtTarget = (String) map.get(key).get(i).get("gmtTarget");
                    String[] s1 = gmtTarget.split(" ");
                    y2 = new Document("_id", s1[0]);
                }
                //在数组后加入新数据 若数据存在 则不会存入
                Document update = new Document().append("$addToSet", new Document().append("records", new Document().append("$each", list2)));
                //获取数据的时间判断其所属数组 并将数据存入对应数组中
                collection.updateMany(y2, update);
                FindIterable<Document> documents1 = collection.find(y2);
                ArrayList<Document> records = null;
                for (Document document1 : documents1) {
                    //获取对应数组相关信息
                    records = (ArrayList<Document>) document1.get("records");
                }
                //System.out.println(records);
                collectionDemo(records);
                //向新数组新增数据后数组对应参数随之改变 给各个参数分别赋值
                Document update2 = new Document().append("$set", new Document().append("count", records.size()).append("gmtStart",
                        records.get(0).get("gmtTarget")).append("gmtEnd", records.get(records.size() - 1).get("gmtTarget")).append("records", records));
                collection.updateOne(y2, update2);
            }
        }
        //释放资源
        mongoClient.close();
    }

2.2 查询

废话不多说 上代码(主要代码):

@RequestMapping(value = "/findMongo", method = RequestMethod.POST)
    public ResponseMessage findMongo(Integer index, @RequestBody Map<String, String> map) {
        if (index == null || index < 1) {
            index = 1;
        }
        MongoClient mongoClient = new MongoClient(host, port);
        MongoDatabase db = mongoClient.getDatabase(map.get("applicationKey"));
        //MongoDatabase db = mongoClient.getDatabase("dgg8be05077d4890910ed2ce8b481");
        //MongoCollection<Document> doc = db.getCollection(map.get("identity"));
        MongoCollection<Document> doc = db.getCollection(map.get("identity"));
        Page<Document> page = new Page<>();
        Map<String, String> mapKey = new HashMap<>();
        for (Map.Entry<String, String> entry : map.entrySet()) {
            if (StringUtils.isNotEmpty(entry.getValue())) {
                mapKey.put(entry.getKey(), entry.getValue());
            }
        }
        String identity = mapKey.get("identity");
        String applicationKey = mapKey.get("applicationKey");
        //Query query = new Query();
        //SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        if (map != null && map.size() != 0 && !mapKey.isEmpty()) {
            // match(相当于 WHERE 或者 HAVING )
            BasicDBObject query = new BasicDBObject();
            List<BasicDBObject> list = new ArrayList<>();

            for (Map.Entry<String, String> entry : mapKey.entrySet()) {
                String key = entry.getKey();
                if (key.equals("identity") || key.equals("applicationKey") || key.equals("pageUrl")) {

                } else {
                    String value = entry.getValue();
                    if (value.contains("~")) {
                        String[] split = value.split("~");
                        String startTime = null;
                        String endTime = null;
                        try {
                            startTime = split[0];
                            endTime = split[1];
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        list.add(new BasicDBObject("records.gmtTarget", new BasicDBObject("$gte", startTime)));
                        list.add(new BasicDBObject("records.gmtTarget", new BasicDBObject("$lte", endTime)));
                    } else {
                        list.add(new BasicDBObject(("records") + "." + key, value));
                    }
                }
            }
            if (list != null && list.size() > 0) {
                BasicDBObject[] array = new BasicDBObject[list.size()];
                list.toArray(array);
                query.append("$and", array);
                BasicDBObject match = new BasicDBObject("$match", query);
                BasicDBObject unwind = new BasicDBObject("$unwind", "$records");
                // BasicDBObject project = new BasicDBObject("$project", new BasicDBObject("count", new BasicDBObject("$size","$records")));
                BasicDBObject project = new BasicDBObject("$project", new BasicDBObject("records", 1));
                //sort(排序)
                BasicDBObject sort = new BasicDBObject("$sort", new BasicDBObject("count", -1));//1:正序,-1倒序
                //skip(跳过前面多少条数据,分页时使用)
                //limt(只要前多少条数据,分页时使用)
                BasicDBObject limit = new BasicDBObject("$limit", 15);
                BasicDBObject skip = new BasicDBObject("$skip", 0);
                List<BasicDBObject> aggregateList = new ArrayList<>();
                aggregateList.add(unwind);
                aggregateList.add(project);
                aggregateList.add(match);
                aggregateList.add(sort);
                aggregateList.add(limit);
                aggregateList.add(skip);
                AggregateIterable<Document> aggregate = mongoClient.getDatabase(applicationKey).getCollection(identity).aggregate(aggregateList);
                List<Document> list1 = new ArrayList<>();
                int totalCount = 0;
                for (Document document : aggregate) {
                    list1.add(document);
                    if (list1 != null) {
                        totalCount++;
                    }
                    int totalPage = totalCount % 15 == 0 ? totalCount / 15 : totalCount / 15 + 1;
                    page.setStartIndex((index - 1) * 15);
                    page.setTotalRecords(totalCount);
                    page.setTotalPage(totalPage);
                    page.setRecords(list1);
                }
                page.setCurrentPage(index);
                page.setPageSize(Page.DEFAULT_VALUE);
                return success(page);
            } else {
                FindIterable<Document> iter = doc.find().skip(0).limit(15);
                int totalCount = 0;
                Page<Document> page1 = new Page<>();
                List<Document> list1 = new ArrayList<>();
                for (Document document : iter) {
                    MongoCursor<Document> iterator = iter.iterator();
                    iterator.next();
                    if (document.get("_id") != null) {
                        totalCount++;
                    }
                    document.remove("_id");
                    Object records = document.get("records");
                    document.put("records", records);
                    list1.add(document);
                    page1.setCurrentPage(index);
                    page1.setPageSize(Page.DEFAULT_VALUE);
                    int totalPage = totalCount % 15 == 0 ? totalCount / 15 : totalCount / 15 + 1;
                    page1.setStartIndex((index - 1) * 15);
                    page1.setTotalRecords(totalCount);
                    page1.setTotalPage(totalPage);
                    page1.setRecords(list1);
                }
                return success(page1);
            }
        } else {
            FindIterable<Document> iter = doc.find().skip(0).limit(15);
            int totalCount = 0;
            Page<Document> page1 = new Page<>();
            List<Document> list = new ArrayList<>();
            for (Document document : iter) {
                MongoCursor<Document> iterator = iter.iterator();
                iterator.next();
                if (document.get("_id") != null) {
                    totalCount++;
                }
                document.remove("_id");
                Object records = document.get("records");
                document.put("records", records);
                list.add(document);
                page1.setCurrentPage(index);
                page1.setPageSize(Page.DEFAULT_VALUE);
                int totalPage = totalCount % 15 == 0 ? totalCount / 15 : totalCount / 15 + 1;
                page1.setStartIndex((index - 1) * 15);
                page1.setTotalRecords(totalCount);
                page1.setTotalPage(totalPage);
                page1.setRecords(list);
            }
            return success(page1);
        }
    }

至此 操作完成

在这里插入图片描述

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
MongoDBTemplate是Spring Data MongoDB提供的一个MongoDB操作工具类,它封装了MongoDB的常用操作,并且提供了对聚合查询的支持。 使用MongoDBTemplate进行聚合查询,需要通过Aggregation对象来构建聚合查询的管道,然后调用MongoDBTemplate的aggregate方法执行查询,例如: ```java Aggregation aggregation = Aggregation.newAggregation( Aggregation.match(Criteria.where("age").gt(18)), Aggregation.group("gender").count().as("total"), Aggregation.project("total").and("gender").previousOperation() ); AggregationResults<BasicDBObject> results = mongoTemplate.aggregate(aggregation, "collectionName", BasicDBObject.class); List<BasicDBObject> resultList = results.getMappedResults(); ``` 上面的示例中,首先使用Aggregation.match方法筛选出年龄大于18岁的数据,然后使用Aggregation.group方法按照性别进行分组并计算每个分组的总数,最后使用Aggregation.project方法只返回分组总数和性别字段。最终查询的结果是一个包含所有分组的总数和性别字段的BasicDBObject列表。 在使用聚合查询时,还可以使用Aggregation类提供的一些其他方法来实现更复杂的查询,例如: - Aggregation.sort:对查询结果进行排序。 - Aggregation.skip和Aggregation.limit:分别用于分页查询。 - Aggregation.lookup:用于关联查询。 - Aggregation.unwind:用于展开嵌套数组字段。 总之,MongoDBTemplate提供了非常便捷的聚合查询功能,可以帮助我们更方便地实现复杂的数据分析和统计功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

白大锅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值