mongodb 内嵌数组分组查询

本文详细介绍了如何使用MongoDB进行内嵌数组的高级查询,包括分组、过滤和聚合操作。通过Java代码和MongoDB查询脚本,展示了如何查询特定用户ID下未读消息的数量,以及如何从内嵌数组中筛选出符合条件的数据项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

mongodb 内嵌数组分组查询(数据结构)

java 代码:

Aggregation aggregation1 = Aggregation.newAggregation(

                    Aggregation.unwind("fnLikes"),
                    Aggregation.match(Criteria.where("user_id").is("2016103160008110180").and("fnLikes.is_read").is(0)),
                    
                    Aggregation.group("$fnLikes.is_read").count().as("未读消息数"));
            AggregationResults<BasicDBObject> outputTypeCount1 =
                    mongoTemplate.aggregate(aggregation1, "t_table_name", BasicDBObject.class);
            for (Iterator<BasicDBObject> iterator = outputTypeCount1.iterator(); iterator.hasNext(); ) {
                DBObject obj = iterator.next();
                System.out.println(JSON.toJSONString(obj));
            }


数据脚本语法:     

db.t_table_name.aggregate([ { "$match" : { "user_id" : "2016103160008110180" , "fnLikes.is_read" : 0}} , { "$unwind" : "$fnLikes"} , { "$group" : { "_id" : "$fnLikes.is_read" , "未读消息数" : { "$sum" : 1}}}])

 

查询内嵌数组(只返回满足条件的数据)

sql命令:

db.t_table_name.find({"user_id":"0601623000120171130"},{"comments":{$elemMatch:{"reply_to_user_id":"0601623000120171130"}}});

 db.t_table_name.aggregate(
  [ { "$match" : { "user_id" : "0400199004520190228"}} , { "$project" : { "comments" : 1}} ,
  { "$unwind" : "$comments"} , { "$match" : { "comments.reply_to_user_id" : "0400199004520190228"}}])

java 代码:

 

方法一:

 //封装对象列表查询条件
        List<AggregationOperation> commonOperations = new ArrayList<>();
        //1. 指定查询主文档
        MatchOperation match = Aggregation.match(Criteria.where("user_id").is(userId));
        commonOperations.add(match);
        //2. 指定投影,返回哪些字段
        ProjectionOperation project = Aggregation.project("comments");
        commonOperations.add(project);
        //3. 拆分内嵌文档
        UnwindOperation unwind = Aggregation.unwind("comments");
        commonOperations.add(unwind);
        //4. 指定查询子文档
        MatchOperation match2 = Aggregation.match(
                Criteria.where("comments.reply_to_user_id").is(userId));
        commonOperations.add(match2);
 
        //创建管道查询对象
        Aggregation aggregation = Aggregation.newAggregation(commonOperations);
        AggregationResults<JSONObject> reminds = mongoTemplate
                .aggregate(aggregation, "t_table_name", JSONObject.class);
        List<JSONObject> mappedResults = reminds.getMappedResults();
        if (mappedResults != null && mappedResults.size() > 0) {
            System.out.println(mappedResults.get(0).getJSONObject("comments").toJSONString());
           /* history = JSONObject
                    .parseObject(mappedResults.get(0).getJSONObject("historyList").toJSONString(), History.class);*/
        }

方法二:

        Aggregation agg = Aggregation.newAggregation(
                Aggregation.unwind("$comments"),//拆分内嵌文档
                Aggregation.match(Criteria.where("user_id").is(userId).and("comments.reply_to_user_id").is(userId)),
                Aggregation.project("comments")
        );
AggregationResults<Object> results = mongoTemplate.aggregate(agg, "t_table_name", Object.class);
List<Object> resultList = results.getMappedResults();
    System.err.println(resultList);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值