国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

java操作mongodb之多表聯(lián)查的實現(xiàn)($lookup)

瀏覽:97日期:2022-08-15 08:38:10

最近在開發(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)!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 克东县| 天峨县| 涞水县| 收藏| 淮阳县| 兴山县| 石阡县| 安福县| 镇安县| 哈巴河县| 灌阳县| 哈巴河县| 裕民县| 深水埗区| 赤城县| 安宁市| 犍为县| 信丰县| 北川| 龙井市| 上杭县| 图木舒克市| 青铜峡市| 来宾市| 英德市| 四子王旗| 木里| 肥东县| 桐梓县| 孙吴县| 浏阳市| 眉山市| 重庆市| 奇台县| 惠水县| 大关县| 鞍山市| 福泉市| 建阳市| 拉萨市| 衡阳市|