NOSQL 不止于SQL,区别于关系数据库。
1、下载
环境是CentOS 7.0,下载的是CentOS7.0版本,注意package选择tgz
# 下载
wget https://fastdl.mongodb.org/linux/mongodb-linux-x86_64-rhel70-7.0.5.tgz
pwd
/data/midware/mongdoDB
tar -zxvf mongodb-linux-x86_64-rhel70-7.0.5.tgz -C /usr/local
mv /usr/local/mongodb-linux-x86_64-rhel70-7.0.5 /usr/local/mongodb
2、给mongodb添加环境变量
# 打开/etc/profile文件
vim /etc/profile
#添加环境变量
export PATH=/usr/local/mongodb/bin:$PATH
#退出,并保存
:wq
#让变更生效
source /etc/profile
3. 创建数据存储目录和日志文件目录
mkdir /var/lib/mongodb
mkdir /var/log/mongodb
4. 编辑mongdb配置文件
//创建配置文件
vim /etc/mongodb.conf
//写入配置内容
dbpath=/var/lib/mongodb //数据存储路径
logpath=/var/log/mongodb/mongod.log //日志文件存储路径
logappend=true //使用追加方式写入日志
port=27017 //端口号
bind_ip=0.0.0.0 //方便外网访问
fork=true //以守护进程方式运行Mongodb,创建服务器进程
//保存退出
:wq
5. 使用配置文件启动MongDB
pwd
/usr/local/mongodb/bin
mongod -f /etc/mongodb.conf
# 关闭
mongod -f /etc/mongodb.conf --shutdown
6、mongoDB GUI
NoSQLBooster 立志做“The Smartest IDE for MongoDB”。
https://www.mongobooster.com/downloads
7、MongoShell
7.1、下载及安装
https://www.mongodb.com/try/download/shell
cd 下载目录
tar -zxvf mongosh-2.1.5-linux-x64.tgz -C /usr/local/
cd /usr/local/
mv mongosh-2.1.5-linux-x64 mongosh
7.2、给mongosh添加环境变量
# 打开/etc/profile文件
vim /etc/profile
#添加环境变量
export PATH=/usr/local/mongosh/bin:$PATH
#退出,并保存
:wq
#让变更生效
source /etc/profile
7.3、运行shell
mongosh "mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+2.1.5"
7.4、问题:vm.max_map_count [65530] is too low
打开/etc/sysctl.conf文件
在文件末尾添加以下一行内容:vm.max_map_count=262144
运行以下命令使配置立即生效: sudo sysctl -p
7.5、问题: Soft rlimits for open file descriptors too low
打开/etc/profile文件
在文件末尾添加以下一行内容:ulimit -n 65535
运行以下命令使配置立即生效: source /etc/profile
7.6、为什么需要mongoDB shell
《MongoDB入门教程》第04篇 MongoDB客户端 - 知乎
8、java 如何像mybatis-plus 那样链式调用?
推荐mongo-Plus,官网地址: Mongo-Plus
定义doc-entity
import com.anwen.mongo.annotation.ID;
import com.anwen.mongo.annotation.collection.CollectionName;
import com.anwen.mongo.enums.IdTypeEnum;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.time.LocalDateTime;
@Data
@AllArgsConstructor
@NoArgsConstructor
@CollectionName(value = "blob")
public class MongoTest {
@ID(type = IdTypeEnum.AUTO)
private String id;
private String userName;
private Long role;
private LocalDateTime createTime;
private String userAddress;
private Long userStatus;
}
定义service
package com.anwen.mongoa.service;
import com.anwen.mongo.service.IService;
import com.anwen.mongoa.entity.MongoTest;
/**
* @author JiaChaoYang
* @project mongo-plus-demo
* @description
* @date 2023-07-18 22:27
**/
public interface MongoService extends IService<MongoTest> {
}
package com.anwen.mongoa.service.impl;
import com.anwen.mongo.service.impl.ServiceImpl;
import com.anwen.mongoa.entity.MongoTest;
import com.anwen.mongoa.entity.User;
import com.anwen.mongoa.service.MongoService;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
/**
* @author JiaChaoYang
* @project mongo-plus-demo
* @description
* @date 2023-07-18 22:27
**/
@Service
public class MongoServiceImpl extends ServiceImpl<MongoTest> implements MongoService {
}
删除
@GetMapping("/deleteUserById")
@ApiOperation("根据id删除用户")
public Object deleteUserById(@RequestParam("id")String id){
return userService.lambdaUpdate().eq(User::getUserId,id).remove();
}
新增
@PostMapping("/addBatchUser")
@ApiOperation("批量添加用户")
public Object addBatchUser(@RequestBody UserDTO userDTO){
return userService.saveBatch(userDTO.getUserList());
}
修改
@GetMapping("/lambdaUpdateUser")
@ApiOperation("链式调用修改")
// @MongoTransactional
public Boolean lambdaUpdateUser(){
boolean update = userService.lambdaUpdate().set(User::getUserStatus, 2).update();
log.info("{}",update);
throw new RuntimeException("asdf");
// return ;
}
分页
@GetMapping("/findAllUserByPage")
@ApiOperation("分页查询所有用户")
public PageResult<User> findAllUser(Integer pageNum, Integer pageSize, @RequestParam(value = "userName",required = false) String userName, @RequestParam(value = "roleName",required = false)String roleName){
Map<Class<?>, ConversionStrategy<?>> allConversion = ConversionService.getAllConversion();
LambdaQueryChainWrapper<User> queryChainWrapper = userService.lambdaQuery().like(StringUtils.isNotBlank(userName), User::getUserName, userName);
queryChainWrapper.orderByDesc(User::getCreateTime);
PageResult<User> page = userService.page(queryChainWrapper, pageNum, pageSize);
User user = page.getContentData().get(0);
page.getContentData().forEach(System.out::println);
return page;
}
统计
@GetMapping("/findAllUser1111")
@ApiOperation("查询所有用户")
public Object findAllUser1111(@RequestParam(value = "userName",required = false)String userName){
// return userService.lambdaQuery().like(StringUtils.isNotBlank(userName), User::getUserName, userName).orderByAsc(User::getCreateTime).list();
return userService.lambdaAggregate().skip(1).limit(10).list();
}
还有更多使用,欢迎去探索吧
代码托管: MongoPlus: 🔥🔥🔥使用MyBatisPlus的方式,优雅的操作MongoDB
GitHub - anwena/MongoPlus: 🔥🔥🔥使用MyBatisPlus的方式,优雅的操作MongoDB
demo:
mongo-plus-demo: mongoplus测试demo
关于mongoDB的性能,后面做分享