在Java中使用MongoDB

一、mongoDB简介

  1. MongoDB 是由 C++ 语言编写的,基于分布式文件存储的数据库,是一个介于关系数据库和非关系数据库之间的产品,是最接近于关系型数据库的 NoSQL 数据库。
    MongoDB 旨在为 WEB 应用提供可扩展的高性能数据存储解决方案。
    MongoDB 将数据存储为一个文档,数据结构由键值(key=>value)对组成。MongoDB 文档类似于JSON对象。字段值可以包含其他文档,数组及文档数组

类似于
在这里插入图片描述
2. MongoDB优点:数据处理能力强,内存级数据库,查询速度快,扩展性强,只是不支持事务
3. 使用场景:
1、应用不需要事务;
2、数据模型无法确定,经常发生变更;
3、应用存储的数据很大达到TB级别以上;
4、应用需要大量的地理位置查询
简单的来说就是数据量比较大,而且主要是查询操作,而且不需要事务支持

二、MongoDB与Mysql的区别

在这里插入图片描述

三、使用

1、安装

安装地址: http://www.mongodb.org/
github: https://github.com/mongodb/
在这里插入图片描述

2、在java中使用

引入依赖

      <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>

配置连接参数

    data:
    mongodb:
      host: 10.0.26.194
      username: wanttop2
      password: Testwanttop2
      port: 27017
      authentication-database: wanttop2
      database: wanttop2

直接使用mongoTemplate
当然也可以自己封装一个工具类

 @Autowired
    private MongoTemplate mongoTemplate;

单表查询

        // 判断 dataType 是否存在,如果存在则添加到查询条件中
        if (performanceRank.getDataType() != null) {
            query.addCriteria(Criteria.where("dataType").is(performanceRank.getDataType()));
        }
        // 判断 divisionCode 是否存在,如果存在则添加到查询条件中
        if (performanceRank.getDivisionCode() != null) {
            query.addCriteria(Criteria.where("divisionCode").is(performanceRank.getDivisionCode()));

        }

        List<SysDimensionDataMarket> sysDimensionDataMarketList = MongoDBUtil.conditionalQuery(query, SysDimensionDataMarket.class, "SysDimensionDataMarket");

3、多表连接查询

mongoDB中没有sql的left join 和right join 的类似的概念而是用到了lookup管道操作符
mongoDB3.2版本新增(聚合管道$lookup操作)
完整代码

    //连表
    LookupOperation cusAndInfoLookup = LookupOperation.newLookup().
                    from("SysProduct").//1.副表表名字
                    localField("prodItemCode").//2.主表的关联字段
                    foreignField("productCode").//3.副表的关联字段
                    as("SysProduct");//4.建议和1一致,结果的别名
    //如果需要多连表就在写一个
    //LookupOperation cusAndInfoLookup1 = LookupOperation.newLookup().
    //                from("SysProduct").//1.副表表名字
    //               localField("prodItemCode").//2.主表的关联字段
    //              foreignField("productCode").//3.副表的关联字段
    //                as("SysProduct");//4.建议和1一致,结果的别名
            //多表的关联条件,查询条件均传入到此
    //创建查询条件
    Criteria criteria = new Criteria();
       if (totalPerformanceCross.getDataCrossType() != null) {
                criteria.and("dataCrossType").is(totalPerformanceCross.getDataCrossType());
            }
            // 判断 areaCode 是否存在,如果存在则添加到查询条件中
            if (totalPerformanceCross.getAreaCode() != null) {
                criteria.and("areaCode").is(totalPerformanceCross.getAreaCode());
            }
            // 判断 companyCode 是否存在,如果存在则添加到查询条件中
            if (totalPerformanceCross.getCompanyCode() != null) {
                criteria.and("companyCode").is(totalPerformanceCross.getCompanyCode());
            }
            //如果查询的字段为附表则需要加上附表名字 
            if (totalPerformanceCross.getDateType() != null) {
                criteria.and("SysProduct.dateType").is(totalPerformanceCross.getDateType());
            }
      //多表的关联条件,查询条件均传入到此
            Aggregation aggregation = Aggregation.newAggregation(
                    //连表条件
                    cusAndInfoLookup,
                    //cusAndInfoLookup1,
                    //查询条件
                    Aggregation.match(criteria),
                    //最后查询结果集显示字段
                    Aggregation.project("pmLineCode", "prodItemCode", "prodItemName", "performance", "growthRate", "monthPerformance", "fullMonthPerformance", "SysProduct.productPicUrl"));
  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值