java操作mongodb之多表聯(lián)查的實(shí)現(xiàn)($lookup)
最近在開發(fā)的過程中,一個(gè)列表的查詢,涉及到了多表的關(guān)聯(lián)查詢,由于持久層使用的是mongodb,對(duì)這個(gè)非關(guān)系型數(shù)據(jù)使用的不是很多,所以在實(shí)現(xiàn)此功能的過程中出現(xiàn)了不少問題,現(xiàn)在此做記錄,一為加深自己的理解,以后遇到此類問題可以快速的解決,二為遇到同樣問題的小伙伴提供一點(diǎn)小小的幫助。
全文分為兩部分:
使用robo3t編寫多表關(guān)系的查詢語句 將編寫的查詢語句整合到j(luò)ava項(xiàng)目多表聯(lián)查的查詢語句:此處使用的為mongodb的robo3t可視化工具,先說下需求:從A(假如說是日志表)表中查詢出符合條件的數(shù)據(jù),根據(jù)A表中符合條件數(shù)據(jù)查詢B(假如說是信息表)表中的數(shù)據(jù),此處也可以將B表的查詢條件加入進(jìn)來(類型于關(guān)系型數(shù)據(jù)庫(kù)中的臨時(shí)表)
mongo查詢語句:
db.getCollection(’A’).aggregate([ { $lookup:{ from:’B’, localField:’userid’, foreignField:’userid’, as:’userinfo’} }, { $unwind:’$userrole’//把一個(gè)數(shù)組展成多個(gè),就比如說按多表連查的userrole數(shù)組中有10數(shù)據(jù),那么用$unwind將把一條帶數(shù)組的數(shù)據(jù)分成10條,這10條數(shù)據(jù)除了userrole不同之外,其它數(shù)據(jù)都是相同的,就類似于一個(gè)展開操作 }, { $match:{’username’:’zhangsan’} }, { $group:{ _id:{ userid:’$userid’,//這個(gè)屬性必須是要A表中有的 userrole:’$userrole.roleid’,//A表中有一個(gè)集合,里面存放的對(duì)象有一個(gè)名為roleid的屬性 }, operateTime:{ $last:’$operateTime’//取A表操作時(shí)間最后一條件數(shù) } info:{ $first:’$userinfo’//因?yàn)閿?shù)組的擴(kuò)展,造成了大量的重復(fù)數(shù)據(jù)(只有userrole不同),$first是只取最新的一條 }} }, { $sort:{’operateTime’:-1}//操作時(shí)間倒序,-1:倒序,1:升序 }, { $skip:0//跳過幾條數(shù)據(jù),也就是從第幾條數(shù)據(jù)開始取 }, { $limit:5//每頁(yè)顯示幾條數(shù)據(jù) }]);
java代碼整合查詢語句
//定義分組字段String[] groupIds = new String[] {'$userid','$userrole.roleid'};//定義查詢條件Criteria criteria = new Criteria();//相當(dāng)于where username = 'zhangsan'criteria.and('username').is('zhangsan');//相當(dāng)于 where age not in('15','20')criteria.and('age').nin('15','20');//in操作對(duì)應(yīng)的語句//criteria.and('').in();//定義排序條件Sort sort = new Sort(Direction.DESC,'operateTime');//聯(lián)合查詢總條數(shù),分頁(yè)用Aggregation aggregationCount = Aggregation.newAggregation( Aggregation.match(criteria);//查詢條件 Aggregation.group(groupIds);//分組字段);//聯(lián)合查詢條件Aggregation newAggregation = Aggregation.newAggregation( Aggregation.lookup(’B’,’userid’,’userid’,’userinfo’),//從表名,主表聯(lián)接字段,從表聯(lián)接字段,別名 Aggregation.unwind('$userrole'), Aggregation.match(criteria), Aggregation.group(groupIds) .last('$operateTime').as('operateTime')//取值,起別名 .first('$userinfo').as('info'), Aggregation.sort(sort), Aggregation.skip(pageSize*(pageNumber-1L)),//Long類型的參數(shù) Aggregation.limit(pageSize));//查詢AggregationResults<BasicDBObject> aggregate = mongoTemplate.aggregate( newAggregation ,'A',BasicDBObject.class//A表,是查詢的主表);int count = mongoTemplate.aggregate(aggregationCount ,'A',BasicDBObject.class).getMappedResults().size();//組裝分頁(yè)對(duì)象Page<BasicDBObject> pager = new Page<>(aggregate.getMappedResults(),count,pageSize,pageNumber,page*(pageNumber-1));//對(duì)象轉(zhuǎn)換將BasicDBObject轉(zhuǎn)換成前面需要的類型.....
到此這篇關(guān)于java操作mongodb之多表聯(lián)查的實(shí)現(xiàn)($lookup)的文章就介紹到這了,更多相關(guān)java mongodb多表聯(lián)查內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. Warning: require(): open_basedir restriction in effect,目錄配置open_basedir報(bào)錯(cuò)問題分析2. ASP中常用的22個(gè)FSO文件操作函數(shù)整理3. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究4. ASP的Global.asa文件技巧用法5. php測(cè)試程序運(yùn)行速度和頁(yè)面執(zhí)行速度的代碼6. html清除浮動(dòng)的6種方法示例7. SharePoint Server 2019新特性介紹8. ASP中if語句、select 、while循環(huán)的使用方法9. React+umi+typeScript創(chuàng)建項(xiàng)目的過程10. Vue+elementUI下拉框自定義顏色選擇器方式
