mongodb @aggregation 返回字段映射不上_「Java」 - SpringBoot & MongoDB

MongoDB是目前热门的非关系数据库,广泛的应用于国内外的互联网公司。

一、MongoDB简介

MongoDB(来自于英文单词Humongous,含义为「庞大」),可以应用于各种规模的企业、各个行业以及各类应用程序的开源数据库,基于分布式文件存储的数据库。C++语言编写,旨在为web应用提供可扩展的高性能数据存储解决方案。

MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。它支持的数据结构非常松散,是类似Json的bjson格式,因此可以存储比较复杂的数据类型。MongoDB最大的特点是支持的查询语言非常强大,语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。

传统的关系数据库一般由数据库(DataBase)、表(Table)、记录(Record)三个层次概念组成;MongoDB是由数据库(DataBase)、集合(Collection)、文档对象(Document)三个层次组成。MongoDB对应关系型数据库里的表,但是集合中没有列、行和关系概念。

MongoDB中的一条记录就是一个文档,是一个数据结构,由字段和值对组成。MongoDB文档与Json对象类似。字段的值有可能包括其他文档、数组及文档数组。MongoDB支持几乎所有主流操作系统,并提供了Java、Python、PHP及C++等编程语言的驱动程序。

MongoDB适合对大量或者无固定格式的数据进行存储,如日志、缓存等。对事物支持较弱,不适用复杂的多文档(多表)的级联查询。

二、MongoDB安装(单点)

订单系统使用了MongoDB,刚好可以通过Spring Boot进行学习,确认了订单系统使用的版本是3.4.2,官网已经是4.x版本,这里还是跟公司环境保持一致。

Download:https://www.mongodb.org/dl/linux/x86_64-rhel62

# 下载解压二进制文件
wget http://downloads.mongodb.org/linux/mongodb-linux-x86_64-rhel62-3.4.2.tgz
tar -vxzf mongodb-linux-x86_64-rhel62-3.4.2.tgz
mv mongodb-linux-x86_64-rhel62-3.4.2 /usr/local/mongodb-3.4.2

# 创建data和log目录
mkdir -p /data01/mongodata /data01/mongolog

# 增加环境变量
vim /etc/profile
    export PATH=/usr/local/mongodb-3.4.2/bin:$PATH
source /etc/profile 

# 增加conf配置
vim mongo.conf
    bind_ip = 10.255.242.129
    port = 27017
    #fork = true

    ## MongoDB Data path
    dbpath=/data01/mongodata

    ## Log path
    logpath=/data01/mongolog/mongodb.log
 
    ## is log Rotate mongoDB 3.0
    logappend = true
    #logRotate = true

    ## is Open journal log
    journal = true

    ## use small files for develop
    smallfiles = true

# 设置启动脚本
vim mongo.sh
    #!/bin/bash
    mongod -fork -config /usr/local/mongodb-3.4.2/mongo.conf
chmod 755 mongo.sh

# 启动MongoDB
sh mongo.sh

通过进程、telnet、以及日志,都能确认MongoDB已经成功启动。

69a52e35e9fb338e6bf60d3216758dc3.png

关于MongoDB集群、配置、运维等总结,以后有机会再进行。这里还是安装后,使用Spring Boot进行操作。

三、spring-boot-starter-data-mongodb

Spring Data是Spring为了简化数据库操作而封装的一个组件包,提供了操作多种数据库的支持,API简洁、调用方便。

spring-boot-starter-data-mongodb是Spring Data的一个子模块。目标是为MongoDB提供一个相近的、一致的、基于Spring的编程模型。spring-boot-starter-data-mongodb核心功能是映射POJO到MongoDB的DBCollection中的文档,并且提供Repository风格数据访问层。除继承Spring Data的通用功能外,针对MongoDB的特性开发了很多定制的功能,使得Spring Boot操作MongoDB更加简便。

MongoDB有两种比较流行的使用方法,一种是直接将Mo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
为什么会这样[user_mongo@nosql01 replicaset]$ cd /opt [user_mongo@nosql01 opt]$ ll total 0 drwxr-xr-x. 3 root root 25 Mar 16 17:08 servers drwxr-xr-x. 2 root root 51 Mar 16 17:10 software [user_mongo@nosql01 opt]$ tar -zxvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/LICENSE-Community.txt: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/MPL-2 tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/MPL-2: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/README tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/README: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/THIRD-PARTY-NOTICES: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/install_compass: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongo: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongod: Cannot open: No such file or directory mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos tar: mongodb-linux-x86_64-rhel70-4.4.12: Cannot mkdir: Permission denied tar: mongodb-linux-x86_64-rhel70-4.4.12/bin/mongos: Cannot open: No such file or directory tar: Exiting with failure status due to previous errors [user_mongo@nosql01 opt]$ tar -zcvf /opt/software/mongodb-linux-x86_64-rhel70-4.4.12.tgz -C /opt/servers/mongodb_demo/replicaset/ tar: Cowardly refusing to create an empty archive Try `tar --help' or `tar --usage' for more information.
06-01

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值