java操作mongodb之多表聯(lián)查的實現(xiàn)($lookup)
最近在開發(fā)的過程中,一個列表的查詢,涉及到了多表的關(guān)聯(lián)查詢,由于持久層使用的是mongodb,對這個非關(guān)系型數(shù)據(jù)使用的不是很多,所以在實現(xiàn)此功能的過程中出現(xiàn)了不少問題,現(xiàn)在此做記錄,一為加深自己的理解,以后遇到此類問題可以快速的解決,二為遇到同樣問題的小伙伴提供一點小小的幫助。
全文分為兩部分:
使用robo3t編寫多表關(guān)系的查詢語句 將編寫的查詢語句整合到j(luò)ava項目多表聯(lián)查的查詢語句:此處使用的為mongodb的robo3t可視化工具,先說下需求:從A(假如說是日志表)表中查詢出符合條件的數(shù)據(jù),根據(jù)A表中符合條件數(shù)據(jù)查詢B(假如說是信息表)表中的數(shù)據(jù),此處也可以將B表的查詢條件加入進來(類型于關(guān)系型數(shù)據(jù)庫中的臨時表)
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ù) } info:{ $first:’$userinfo’//因為數(shù)組的擴展,造成了大量的重復(fù)數(shù)據(jù)(只有userrole不同),$first是只取最新的一條 }} }, { $sort:{’operateTime’:-1}//操作時間倒序,-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)查的實現(xiàn)($lookup)的文章就介紹到這了,更多相關(guān)java mongodb多表聯(lián)查內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向2. windows服務(wù)器使用IIS時thinkphp搜索中文無效問題3. PHP設(shè)計模式中工廠模式深入詳解4. 淺談python出錯時traceback的解讀5. .NET中l(wèi)ambda表達式合并問題及解決方法6. Python importlib動態(tài)導(dǎo)入模塊實現(xiàn)代碼7. python matplotlib:plt.scatter() 大小和顏色參數(shù)詳解8. Ajax實現(xiàn)表格中信息不刷新頁面進行更新數(shù)據(jù)9. 利用promise及參數(shù)解構(gòu)封裝ajax請求的方法10. JSP數(shù)據(jù)交互實現(xiàn)過程解析
