MongoDB多表聯查


Oracle數據庫: select * from (select t1.name,t2.name from student t1, class t2 where t1.id=t2.id) F where 1=1;

MongoDB: 什么鬼?

非關系型數據庫多表聯查讓人崩潰

@Autowired
    private MongoTemplate mongoTemplate;

@RequestMapping(value = "/queryByMap.html", method = RequestMethod.GET)
    public Map<String, Object> queryByMap() {
        //定義分組字段
        String[] groupIds = new String[] {"$_id","$name","$msg","$itemList",
                "$instanceItem.elementName",
                "$instanceItem"};
        //定義查詢條件
        Criteria criteria = new Criteria();
        //相當於where username = "zhangsan"
        criteria.and("name").is("name0");
        //相當於 where age not in("15","20")
        //criteria.and("age").nin("15","20");
        criteria.and("msg").is("message0");
        //in操作對應的語句
        //criteria.and("").in();
        //定義排序條件
        List<Sort.Order> orders = new ArrayList();
        orders.add(new Sort.Order(Sort.Direction.DESC, "name"));
        Sort sort = Sort.by(orders);

        //聯合查詢條件
        Aggregation newAggregation = Aggregation.newAggregation(
                Aggregation.lookup("UDT_TestInstanceItem","name","elementName","instanceItem"),
                //從表名,主表聯接字段,從表聯接字段,別名

                Aggregation.unwind("$itemList"),
                Aggregation.match(criteria),
                Aggregation.group(groupIds)
                        .last("$name").as("名稱")//取值,起別名
                        .first("$instanceItem.elementName").as("父項名稱")
                        .first("$instanceItem.msg").as("子項msg")
                        .first("$instanceItem.name").as("子項名稱"),
                Aggregation.sort(sort),
                Aggregation.skip(0),//Long類型的參數
                Aggregation.limit(100)
        );

        //查詢
        AggregationResults<BasicDBObject> aggregate = mongoTemplate.aggregate(
                newAggregation ,"UDT_TestInstance",BasicDBObject.class //A表,是查詢的主表
        );
        int count = mongoTemplate.aggregate(newAggregation ,"UDT_TestInstance",BasicDBObject.class).getMappedResults().size();

        Map<String, Object> result = new HashMap<>();
        result.put("result", aggregate.getMappedResults());
        result.put("count", count);
        return result;
    }

 上面的多表聯查方法看起來有點繁瑣,而且我在生產環境中運用的時候總是查不出數數據(demo可以查出數據),所以用了下面的方法:

 

 1 LookupOperation lookupOperation=LookupOperation.newLookup().
 2                     from("DYNC_EXT_TestInstanceItem").  //關聯從表名
 3                     localField("partVersion").     //主表關聯字段
 4                     foreignField("partVersion").//從表關聯的字段
 5                     as("result");   //查詢結果名
 6 AggregationOperation match = Aggregation.match(criteria);
 7 Aggregation aggregation=Aggregation.newAggregation(match, lookupOperation); //多條件
 8 List<Map> results = mongoTemplate.aggregate(aggregation,"DYNC_EXT_TestInstance", Map.class).getMappedResults();
 9 //上面的DYNC_EXT_TestInstance必須是查詢的主表名
10 System.out.println(JSON.toJSONString(results));

這種寫法閱讀起來簡單,有效適合小白

還有一種方法是利用Java中的集合,分別查出多張表的數據存在List集合中,在通過循環比較得出符合的數據。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM