springBoot整合MongoDB复杂查询条件的实现

SpringBoot workingTime 113℃ 0评论

Unknown-3

我又要吐槽了,这次吐槽以下几点:

- 现在烂大街的所谓的架构师,没几个靠谱的,自己都没填过坑的技术就敢说精通,我***。
- 不知道是MongoDB国内用的人太少,还是因为最后大家都放弃了,反正国内博客里面资料都是非常过时的东西。
- 还有一点我需要吐槽,**MongoDB真的不适合做复杂场景的业务DB,因为MongoDB相对MySQL存在很多不足,其次就是学习曲线真的太高了。** 最最适合的场景我感觉就是作为爬虫的数据存放点。[打个广告,想学JAVA爬虫的朋友点这里](http://www.qnloft.com/ppt)

NoSql

进入正题,先来看一下这次的主角,一段比较复杂的nosql。

db.medical_examination_info.aggregate([
{
    $match: { "institute_id": { $in: ["750" , "751"] } , 'examination_index.department_id' : {$in : [3]}}
},
{
    $lookup:{
        from : "collecting_sample",
        localField: "work_no",
        foreignField: "work_no",
        as: "inventory_docs"
    }

}
,    
{
    $unwind:
        { path: "$inventory_docs" }
    
}
,    
{
    $unwind:
        { path: "$inventory_docs.containers"}
    
}
,
{
    $group : 
        {_id : 
            {'id' : '$inventory_docs.containers.id' , 
                'name' : '$inventory_docs.containers.name' ,
             'colorCode' : '$inventory_docs.containers.sample_color_code'   
            } 
            , priorities : {$push :'$priority'}
            , count : {$sum : 1}      
        }
} 
])

简单说一下这个nosql的意思,medical_examination_infocollecting_sample表,用work_no字段做关联查询,将数据根据条件筛选后做分组统计。

辛辛苦苦,冥思苦想写好的nosql,转换成java dao的时候…..
这里写图片描述
此处应该播放一下 刘欢老师的《从头再来》表达一下我的心情!!!

springboot nosql dao

@Override
public List<CollectingSampleGroupView> findCollectingGroup(CollectingSampleGroupSearch collectingSampleGroupSearch) {

    Criteria criteria = where(Constants.ITEM_EXAMINATION_INSTITUTE_ID).in(collectingSampleGroupSearch.getInstituteIds());
    criteria.and("examination_index.department_id").in(collectingSampleGroupSearch.getDeptIds());
    AggregationOperation match = new MatchOperation(criteria);

    Field from = Fields.field("collecting_sample");
    Field localField = Fields.field("work_no");
    Field foreignField = Fields.field("work_no");
    Field as = Fields.field("inventory_docs");
    AggregationOperation lookUp = new LookupOperation(from ,localField ,foreignField ,as);

    AggregationOperation unwindDoc = new UnwindOperation(Fields.field("$inventory_docs"));
    AggregationOperation unwindCon = new UnwindOperation(Fields.field("$inventory_docs.containers"));

    Field idField = Fields.field("id" , "inventory_docs.containers.id");
    Field nameField = Fields.field("name" , "inventory_docs.containers.name");
    Field colorField = Fields.field("colorCode" , "inventory_docs.containers.sample_color_code");

    GroupOperation groupOperation =
            new GroupOperation(Fields.from(idField ,nameField ,colorField))
                    .push("$priority").as("priorities")
                    .count().as("checkSum");

    return mongoTemplate.aggregate(Aggregation.newAggregation(match ,lookUp , unwindDoc ,unwindCon ,groupOperation),
            Constants.MEDICAL_EXAMINATION_INFO,
            CollectingSampleGroupView.class).getMappedResults();
}

是不是看着有种崩溃的冲动!!!大家注意了(敲黑板),代码我们一段一段的拆解

{
    $match: { "institute_id": { $in: ["750" , "751"] } , 'examination_index.department_id' : {$in : [3]}}
}

这段查询条件对应代码:

Criteria criteria = where("institute_id").in(collectingSampleGroupSearch.getInstituteIds());
criteria.and("examination_index.department_id").in(collectingSampleGroupSearch.getDeptIds());
AggregationOperation match = new MatchOperation(criteria);

lookup函数是表关联查询

{
    $lookup:{
        from : "collecting_sample",
        localField: "work_no",
        foreignField: "work_no",
        as: "inventory_docs"
    }

}

对应的java代码是:

Field from = Fields.field("collecting_sample");
Field localField = Fields.field("work_no");
Field foreignField = Fields.field("work_no");
Field as = Fields.field("inventory_docs");
AggregationOperation lookUp = new LookupOperation(from ,localField ,foreignField ,as);

unwind是将数组中的指定元素拆分为单独文档

{
    $unwind:
        { path: "$inventory_docs" }
    
}
,    
{
    $unwind:
        { path: "$inventory_docs.containers"}
    
}

对应的java代码如下:

AggregationOperation unwindDoc = new UnwindOperation(Fields.field("$inventory_docs"));
AggregationOperation unwindCon = new UnwindOperation(Fields.field("$inventory_docs.containers"));

group就是这次的重点了,对文档进行分组,然后统计!push是在数组中追加一段数据

{
    $group : 
        {_id : 
            {'id' : '$inventory_docs.containers.id' , 
             'name' : '$inventory_docs.containers.name' ,
             'colorCode' : '$inventory_docs.containers.sample_color_code'   
            } 
            , priorities : {$push :'$priority'}
            , count : {$sum : 1}      
        }
} 

对应的java代码如下:

Field idField = Fields.field("id" , "inventory_docs.containers.id");
Field nameField = Fields.field("name" , "inventory_docs.containers.name");
Field colorField = Fields.field("colorCode" , "inventory_docs.containers.sample_color_code");

GroupOperation groupOperation =
        new GroupOperation(Fields.from(idField ,nameField ,colorField))
.push("$priority").as("priorities").count().as("checkSum");

最后,将上面的AggregationOperation放入mongoTemplate里面转后,执行,再讲结果转成对应的实体类:

mongoTemplate.aggregate(Aggregation.newAggregation(match ,lookUp , unwindDoc ,unwindCon ,groupOperation),
Constants.MEDICAL_EXAMINATION_INFO,CollectingSampleGroupView.class).getMappedResults();

总结

我想大家从这个还不算复杂的nosql,差不多就能看出来这东西《MongoDB从入门到放弃》。

最后附上:MongoDB官方文档

转载请注明:R&M » springBoot整合MongoDB复杂查询条件的实现

喜欢 (0)or分享 (0)
发表我的评论
取消评论

表情

联系我:rm@rmworking.com