java操作mongodb之多表聯(lián)查的實(shí)現(xiàn)($lookup)
最近在開發(fā)的過程中,一個列表的查詢,涉及到了多表的關(guān)聯(lián)查詢,由于持久層使用的是mongodb,對這個非關(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ù)庫中的臨時(shí)表)
mongo查詢語句:
db.getCollection(’A’).aggregate([ { $lookup:{ from:’B’, localField:’userid’, foreignField:’userid’, as:’userinfo’} }, { $unwind:’$userrole’//把一個數(shù)組展成多個,就比如說按多表連查的userrole數(shù)組中有10數(shù)據(jù),那么用$unwind將把一條帶數(shù)組的數(shù)據(jù)分成10條,這10條數(shù)據(jù)除了userrole不同之外,其它數(shù)據(jù)都是相同的,就類似于一個展開操作 }, { $match:{’username’:’zhangsan’} }, { $group:{ _id:{ userid:’$userid’,//這個屬性必須是要A表中有的 userrole:’$userrole.roleid’,//A表中有一個集合,里面存放的對象有一個名為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//每頁顯示幾條數(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操作對應(yīng)的語句//criteria.and('').in();//定義排序條件Sort sort = new Sort(Direction.DESC,'operateTime');//聯(lián)合查詢總條數(shù),分頁用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();//組裝分頁對象Page<BasicDBObject> pager = new Page<>(aggregate.getMappedResults(),count,pageSize,pageNumber,page*(pageNumber-1));//對象轉(zhuǎn)換將BasicDBObject轉(zhuǎn)換成前面需要的類型.....
到此這篇關(guān)于java操作mongodb之多表聯(lián)查的實(shí)現(xiàn)($lookup)的文章就介紹到這了,更多相關(guān)java mongodb多表聯(lián)查內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 基于PHP做個圖片防盜鏈2. ASP.NET MVC使用Boostrap實(shí)現(xiàn)產(chǎn)品展示、查詢、排序、分頁3. XML在語音合成中的應(yīng)用4. asp.net core 認(rèn)證和授權(quán)實(shí)例詳解5. .NET中實(shí)現(xiàn)對象數(shù)據(jù)映射示例詳解6. php使用正則驗(yàn)證密碼字段的復(fù)雜強(qiáng)度原理詳細(xì)講解 原創(chuàng)7. ASP.NET MVC把數(shù)據(jù)庫中枚舉項(xiàng)的數(shù)字轉(zhuǎn)換成文字8. 如何使用ASP.NET Core 配置文件9. jscript與vbscript 操作XML元素屬性的代碼10. 基于javaweb+jsp實(shí)現(xiàn)企業(yè)車輛管理系統(tǒng)
