mongo之聚合

表结构:

{
    "_id" : NumberLong(4306182),
    "id" : "xxxxx",
    "user_id" : "xxxxx",
    "entry_date" : ISODate("2015-10-30T14:59:57.000+08:00"),
    "goods_id" : xxx,
    "goods_verno" : xx,
    "goods_name" : "测试商品",
    "powers" : [ 
        {
            "end_date" : ISODate("2015-11-25T00:00:00.000+08:00"),
            "days" : 30,
            "product_id" : 245,
            "max_child_count" : 4,
            "is_parent_manager" : 1,
            "product_name" : "测试产品",
            "funs" : [ 
                785, 
                820, 
                821, 
                822, 
                823, 
                824, 
                825, 
                826, 
                827, 
                828, 
                829, 
                830, 
                831, 
                832, 
                833, 
                834, 
                835, 
                836
            ],
            "code" : "201503",
            "start_date" : ISODate("2015-10-30T00:00:00.000+08:00"),
            "max_pdu_count" : 8
        }
    ]
}


查询方式:

db.function_power_data.aggregate(

{
    "$match":{"user_id":"109095","powers":{"$ne":[]}}//查询条件
},
{
    "$project":{"_id":0,"entry_date":1,"goods_id":1,"powers":1}//显示字段
},
{
    "$unwind":"$powers"//拆分数组
},
{
    "$group":{"_id":"$goods_id","entry_date":{"$max":"$entry_date"},"end_date":{"$max":"$powers.end_date"}}//分组统计
},
{
    "$sort":{"_id":1}   //排序
},
{
    "$skip":1//跳过前几条数据
},
{
    "$limit":2//显示记录数
}

);


统计函数:

表达式 描述 实例
$sum 计算总和。 db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$sum : "$likes"}}}])
$avg 计算平均值 db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$avg : "$likes"}}}])
$min 获取集合中所有文档对应值得最小值。 db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$min : "$likes"}}}])
$max 获取集合中所有文档对应值得最大值。 db.mycol.aggregate([{$group : {_id : "$by_user", num_tutorial : {$max : "$likes"}}}])
$push 在结果文档中插入值到一个数组中。 db.mycol.aggregate([{$group : {_id : "$by_user", url : {$push: "$url"}}}])
$addToSet 在结果文档中插入值到一个数组中,但不创建副本。 db.mycol.aggregate([{$group : {_id : "$by_user", url : {$addToSet : "$url"}}}])
$first 根据资源文档的排序获取第一个文档数据。 db.mycol.aggregate([{$group : {_id : "$by_user", first_url : {$first : "$url"}}}])
$last 根据资源文档的排序获取最后一个文档数据 db.mycol.aggregate([{$group : {_id : "$by_user", last_url : {$last : "$url"}}}])

聚合框架中管道概念常用的几个操作:

  • $project:修改输入文档的结构。可以用来重命名、增加或删除域,也可以用于创建计算结果以及嵌套文档。
  • $match:用于过滤数据,只输出符合条件的文档。$match使用MongoDB的标准查询操作。
  • $limit:用来限制MongoDB聚合管道返回的文档数。
  • $skip:在聚合管道中跳过指定数量的文档,并返回余下的文档。
  • $unwind:将文档中的某一个数组类型字段拆分成多条,每条包含数组中的一个值。
  • $group:将集合中的文档分组,可用于统计结果。
  • $sort:将输入文档排序后输出。
  • $geoNear:输出接近某一地理位置的有序文档。


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!对于MongoTemplate的聚合查询,您可以使用Aggregation类来构建聚合查询管道。以下是一个简单的示例: ```java import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.aggregation.Aggregation; import org.springframework.data.mongodb.core.aggregation.TypedAggregation; import org.springframework.data.mongodb.core.aggregation.AggregationResults; import static org.springframework.data.mongodb.core.aggregation.Aggregation.*; public class AggregationExample { private MongoTemplate mongoTemplate; public AggregationExample(MongoTemplate mongoTemplate) { this.mongoTemplate = mongoTemplate; } public void performAggregation() { TypedAggregation aggregation = newAggregation( // 添加聚合操作 match(Criteria.where("fieldName").is("value")), group("groupField").count().as("count"), sort(Sort.Direction.DESC, "count") ); AggregationResults<AggregateResultClass> results = mongoTemplate.aggregate(aggregation, "collectionName", AggregateResultClass.class); List<AggregateResultClass> resultList = results.getMappedResults(); // 处理结果 for (AggregateResultClass result : resultList) { // 进行相应的操作 // result.getGroupField(); // result.getCount(); } } } // 聚合结果的POJO类 class AggregateResultClass { private String groupField; private int count; // 省略构造函数、getter和setter } ``` 请根据您的具体需求修改上述示例中的字段和条件。希望对您有所帮助!如果您有任何其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值