mongodb 内嵌数组分组查询
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);

相关文章: