zoukankan      html  css  js  c++  java
  • 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集合中,在通过循环比较得出符合的数据。

  • 相关阅读:
    IFS二次开发03——Item
    TFS二次开发01——TeamProjectsPicher
    我的八年程序之路(二)三月方便面换来800月薪
    我的八年程序之路(一)求职的艰辛
    重温CSS之背景、文本样式
    重温CSS之文档结构
    重温CSS之基础
    不使用第三方软件、使用IE11自带功能来屏蔽浏览器广告
    Windwos8.1下配置PHP环境
    Python学习系列之(二)图解Windows8.1下安装Django
  • 原文地址:https://www.cnblogs.com/mxh-java/p/11853760.html
Copyright © 2011-2022 走看看