MongoDB 一些特殊的语法

1、多个字段合计

/**
     * 查询书本课件
     */
    public Page<Document> listPage(String name, String resourceType, Long createTime, int pageIndex, int limit) {
        Document query = doc();

        if (StringUtils.isNotBlank(name)) {
            query.append("fileName", doc("$regex", name));
        }

        if (StringUtils.isNotEmpty(resourceType)) {
            query.put("resourceType", resourceType);
        }

        if (null != createTime) {
            query.put("createTime", createTime);
        }

        long total = bookCoursewareResourceCollection.countDocuments(query);
        Page<Document> page = new Page<>();
        page.setTotal((int) total);
        page.setSize(limit);

        // 查询指定页记录
        FindIterable<Document> iterable = bookCoursewareResourceCollection.find(query)
                .skip((pageIndex - 1) * limit).limit(limit)
                .sort(doc("createTime", -1));
        List<Document> list = MongoUtil.toList(iterable);

        // 查询总计;
        List<Document> aggregates = new ArrayList<>(Arrays.asList(
                $match(query),
                $group(doc("_id", 0)
                        .append("downTotalNum", doc("$sum", "$downloadCount"))
                        .append("viewTotalNum", doc("$sum", "$viewCount"))
                        .append("likeTotalNum", doc("$sum", "$favoriteCount")))));
        Document result = bookCoursewareResourceCollection.aggregate(aggregates).first();

        Document document = new Document();
        document.put("fileName", "总计");
        document.put("downloadCount", result.getInteger("downTotalNum"));
        document.put("viewCount", result.getInteger("viewTotalNum"));
        document.put("favoriteCount", result.getInteger("likeTotalNum"));
        document.put("createUserId", 0);
        list.add(document);
        page.setRecords(list);

        return page;
    }

2、根据两个字段相加后来排序

    public Page<Document> listPage(String title, Integer status, Long startTime, Long endTime,
                                   int pageIndex, int limit) {
        Document query = doc();
        Document doc = doc();

        if (StringUtils.isNotBlank(title)) {
            // $options 大小写不敏感
            query.append("title", doc("$regex", title).append("$options", "i"));
        }
        if (null != status) {
            query.append("status", status);
        }
        if (null != startTime) {
            doc.put("$gte", startTime);
        }
        if (null != endTime) {
            doc.put("$lte", endTime);
        }
        if (!doc.isEmpty()) {
            query.append("createTime", doc);
        }
        long total = newsCollection.countDocuments(query);
        Page<Document> page = new Page<>();
        page.setTotal((int) total);
        page.setSize(limit);

        // 查询指定页记录;  按照排序号倒序排列,按总浏览量倒叙,按发布时间降序排列   -1代表倒序
        List<Document> aggregates = new ArrayList<>(Arrays.asList($match(query))
        );
        aggregates.add(new Document("$addFields", new Document("count", new Document("$add", Arrays.asList("$viewCount", "$virtualViewCount")))));
        aggregates.add(new Document("$sort", doc("orderNum", -1).append("count", -1).append("createTime", -1)));
        aggregates.add(new Document("$skip", (pageIndex - 1) * limit));
        aggregates.add(new Document("$limit", limit));

        List<Document> documents = new ArrayList<>();
        AggregateIterable<Document> aggregate = newsCollection.aggregate(aggregates);
        for (Document document : aggregate) {
            document.put("id", document.getObjectId("_id").toHexString());
            document.remove("_id");
            Long createTimeTemp = document.getLong("createTime");
            String createTime = DateUtil.formatDateTime(new Date(createTimeTemp));
            document.put("createTime", createTime);
            documents.add(document);
        }
        page.setRecords(documents);
        return page;
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

十方天士

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

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

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

打赏作者

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

抵扣说明:

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

余额充值