【问题标题】:How to implement this query in spring query custom mongoDB using mongotemplate如何使用 mongotemplate 在 spring 查询自定义 mongoDB 中实现此查询
【发布时间】:2021-05-14 09:46:32
【问题描述】:
db.getCollection('gift').aggregate([
     { $match: {nick_name: { $ne: '' }} },
     { $project : {_id: 0, nick_name : 1, price: { $toLong: "$price" }, unit: { $toLong: 1000 }, use: { $toLong: 1 }} },
     { $project : {nick_name : 1, price: { $multiply: ["$price", "$unit"] }, use: 1} },
     { $group: {_id: '$nick_name', price: { $sum: '$price' }, total: { $sum: '$use' }} } ]);

这是我的代码

 public List<GiftCodeAggregate> statistic() {
        Criteria criteria = Criteria.where("nick_name").ne("");
        MatchOperation matchOperation = Aggregation.match(criteria);

        //ProjectionOperation projectStage = Aggregation.project("nickname", "price");

        //ProjectionOperation projectPrice = Aggregation.project("price").and(ConvertOperators.ToLong.toLong("price")).as("price");
        //ProjectionOperation projectUnit = Aggregation.project("unit").and(ConvertOperators.ToLong.toLong(1000)).as("unit");        ProjectionOperation projectUse = Aggregation.project("use").and(ConvertOperators.ToLong.toLong(1)).as("use");        //ProjectionOperation projectNickname = Aggregation.project("nick_name", "price", "use").andInclude("nick_name");
        //ProjectionOperation projectTotal = Aggregation
                .project( "price", "use").and("use").multiply((Number) Fields.field("price")).as("total");

        //ProjectionOperation projectTest = Aggregation.project("price")
                .and(ConvertOperators.ToLong.toLong("price")).as("price");

        //GroupOperation groupOperation = Aggregation
                .group("nickname")
                .sum("price").as("price")
                .sum("total").as("use");

        //List<AggregationOperation> aggregationOperationList = Arrays.asList(matchOperation, projectStage, projectPrice, projectUnit, projectUse, projectNickname, projectTotal);
        List<AggregationOperation> aggregationOperationList = Arrays.asList(matchOperation, projectTest);
        TypedAggregation<GiftCodeEntity> aggregation = Aggregation.newAggregation(GiftCodeEntity.class, aggregationOperationList);
        AggregationResults<GiftCodeAggregate> result = this.mongoTemplate.aggregate(aggregation, GiftCodeAggregate.class);
        return result.getMappedResults();
}

我在将字段价格从字符串转换为计算时遇到问题/错误,看起来我的代码有问题?

【问题讨论】:

  • 记录样本:{ "_id" : ObjectId("543253"), "ticket" : "9JDD567", "price" : "10", "quantity" : 5, "count_use" : 1 ,“昵称”:“昵称”,“create_time”:“2020-11-08 20:29:54”}

标签: spring-boot spring-data-mongodb mongotemplate


【解决方案1】:

试试这个:

public List<GiftCodeAggregate> statistic() {
    MatchOperation match         = Aggregation.match(Criteria.where("nick_name").ne(""));

    ProjectionOperation project1 = Aggregation.project("nickname")
        .and(ConvertOperators.ToLong.toLong("price")).as("price")
        .and(ConvertOperators.ToLong.toLong(1000)).as("unit")
        .and(ConvertOperators.ToLong.toLong(1)).as("use");     

    ProjectionOperation project2 = Aggregation.project("nickname", "use")        
        .and(ArithmeticOperators.Multiply.valueOf("price").multiplyBy("unit")).as("price");

    GroupOperation group         = Aggregation.group("nickname")
        .sum("price").as("price")
        .sum("use").as("total");

    Aggregation aggregation = Aggregation.newAggregation(match, project1, project2, group);

    return mongoTemplate
        .aggregate(aggregation, GiftCodeEntity.class, GiftCodeAggregate.class)
        .getMappedResults();
}

精简版:

MatchOperation match         = Aggregation.match(Criteria.where("nick_name").ne(""));

ProjectionOperation project  = Aggregation.project("nickname")
    .and(ArithmeticOperators.Multiply.valueOf("price").multiplyBy(1000)).as("price")
    .and(ConvertOperators.ToLong.toLong(1)).as("use");

GroupOperation group         = Aggregation.group("nickname")
    .sum("price").as("price")
    .sum("use").as("total");

Aggregation aggregation = Aggregation.newAggregation(match, project, group);

【讨论】:

    猜你喜欢
    • 2019-07-27
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    • 2017-11-06
    • 2017-03-28
    • 2014-11-15
    • 1970-01-01
    • 2017-07-20
    相关资源
    最近更新 更多