windows安装
进入到这个页面,如果没有下载,F5刷新到下载
解压到指定文件夹内
两种启动方式:
1、命令行+参数
mongod --dbpath=…\data\db
2、命令行+配置文件
图形界面下载地址:https://www.mongodb.com/download-center/compass
Linux安装
下载地址
先把安装包上传到cd /data 目录下
1.在usr/local/下创建mongodb文件夹
cd /usr/local/
mkdir mongodb
2.解压文件
tar -xzvf mongodb-linux-x86_64-rhel70-4.0.1.tgz
3.将解压后的文件下所有内容移动到mongodb文件夹下
mv mongodb-linux-x86_64-rhel70-4.0.1/* /usr/local/mongodb/
4.添加mongodb的环境变量
vim/etc/profile
export MONGODB_HOME=/usr/local/mongodb
export PATH= P A T H : PATH: PATH:MONGODB_HOME/bin
5.修改保存后要重启系统配置
source /etc/profile
接下来创建mongodb数据文件和日志文件的存放位置,并且对启动项进行配置
6.创建数据库文件存放路径
cd /usr/local/mongodb
mkdir -p data/db
chmod -R 777 data/db
7.创建日志文件
mkdir logs
cd logs
touch mongodb.log
8.创建启动文件
cd /usr/local/mongodb/bin
vim mongodb.conf
systemLog:
#mongoDB发送所有日志输出的目标指定为文件
destination: file
#mongod或mongos应向其发送所有诊断日志记录信息的日志文件的路径
path: "/usr/local/mongodb/logs/mongodb.log"
#mongod或mongod实例重新启动时,mongos或mongod会将新条目附加到现在的日志文件的末尾
logAppend: true
storage:
#mongod实例存储其数据的目录。storage.dbPath设置仅适用于mongod。
dbPath: "/usr/local/mongodb/data/db"
journal:
#启用或禁用持久化日志以确保数据文件保持有效和可恢复
enabled: true
processManagement:
fork: true
net:
#服务实例绑定的IP,默认是localhost,内网
bindIp: localhost,172.16.0.5
#绑定端口
port: 27017
启动数据库
切换到bin目录下:
cd /usr/local/mongodb/bin
启动数据库
mongod -f mongodb.conf
访问数据库
mongo
目前没有设置权限,后期再设置
常用命令
show dbs 或者(show databases) 查看所有磁盘库
use articledb #创建新的数据库articledb
创建库规则:
不能是空字符串
不得含有不规范字符(空格、.、$、/、\)
应全部小写
最多64字节
默认库的意义
admin:从权限的角度来看,这是“root”数据库,要是将一个用户添加到这个数据库,这用户自动继承所有数据库的权限,一些特定的服务器端命令也只能从这个数据库运行,比如列出所有的数据库或者关闭服务器
local:这个数据库永远不会被复制,可以用来存储限于本地单台服务器的任意集合
config:当Mongo用于分片设置时,config数据库在内部使用,用于保存分片的相关信息
数据库删除
db.dropDatabases() 提示:主要用来删除已经持久化的数据库
集合的显式创建(了解)
基本语法:
db.createCollection(“mycollection”)
查看当前库中的表:show tables命令
show collections
show tables
删除集合
db.collection.drop()
文档的基本CRUD
db.comment.insert() //单个文档插入
db.comment.find() //查询文档
db.comment.insertMany() //插入多个文档
db.comment.findOne() //查询文档的第一条数据
db.comment.find({},{}) //第一个{}里是条件。第二个{}里是要显示的字段
db.comment.update(query,update,options) //query需要修改的数据条件,update修改的内容,
db.comment.update({},{}) //修改的内容为覆盖性的,既内容全为{}里的内容 默认修改一条
db.comment.update({},{$set:{}}) //修改为指定性的,既只修改指定的字段内容 默认修改一条
db.comment.update({},{$set:{}},{multi:true}) //修改为指定性的,既只修改指定的字段内容,批量修改
db.comment.update({},{$inc:{}}) //修改为指定性的,既指定的字段增加指定值 默认修改一条
db.comment.remove(条件) //删除指定条件的内容
db.comment.remove({}) //删除所有
分页查询
db.comment.count() //统计所有记录数
db.comment.count({条件}) //统计指定条件记录数
db.comment.find().limit(m) //返回从第一条起的m条数据
db.comment.find().limit(m).skip(n) //返回从第n+1条起的m条数据
排序
db.comment.find().sort(key:1) //安照指定字段升序排列,(1、升序,-1、降序)
复杂的查询
db.comment.find({key://}) // 正则查询
db.comment.find({key:{$gt:value}}) //大于 key的值>value
db.comment.find({key:{$lt:value}}) //小于 key的值<value
db.comment.find({key:{$gte:value}}) //大于等于 key的值>=value
db.comment.find({key:{$lte:value}}) //小于等于 key的值<=value
db.comment.find({key:{$ne:value}}) //不等于 key的值!=value
db.comment.find({key:{$in:[v1,v2]}}) //包含v1,v2
db.comment.find({key:{$nin:[v1,v2]}}) //不包含v1,v2
db.comment.find($and[{条件1},{条件2}]) //条件并连接查询
db.comment.find($or[{条件1},{条件2}]) //条件或连接查询
x
show collections db.task_conf.update({},{$set:{'push_status':false}},{multi:true})db.task_details.update({},{$set:{'exec_status':false}},{multi:true})
Java中聚合操作
public List<TaskDetails> findPageByActiveTag3(Integer activeTag, SpringbootPageable pageable) {
LookupOperation lookupConf = LookupOperation.newLookup().
from("task_conf_business").//关联表名 lots
localField("task_conf_number").//关联字段
foreignField("task_conf_number").//主表关联字段对应的次表字段
as("conf"); //别名
LookupOperation lookupStar = LookupOperation.newLookup().
from("radar_star_business").//关联表名 radar_star_business
localField("conf.buid").//关联字段
foreignField("buid"). //主表关联字段对应的次表字段
as("starData"); //别名
Criteria criteria = new Criteria();
criteria.and("active_tag").is(activeTag); //当天
criteria.and("active_status").is(false);
criteria.and("expire_status").is(false);
criteria.and("followup_interval").is(2);
MatchOperation match = Aggregation.match(criteria); //条件
SortOperation sort = Aggregation.sort(new Sort(Sort.Direction.DESC, "starData.star")); //排序
SkipOperation skip = Aggregation.skip(pageable.getOffset()); //起始位置
LimitOperation limit = Aggregation.limit(pageable.getPageSize()); //每页多少条
AggregationOptions build = AggregationOptions.builder().allowDiskUse(true).build(); //处理排序超过内存
Aggregation newAggregation = Aggregation.newAggregation(
lookupConf, match, lookupStar, sort, skip, limit
); //创建聚合函数
newAggregation.withOptions(build);
AggregationResults<TaskDetails> task_details_business = mongoTemplate.aggregate(newAggregation, "task_details_business", TaskDetails.class);
return task_details_business.getMappedResults();
}