Mongodb学习
文章目录
Mongodb介绍
- 业务使用场景
- 具体使用场景
- 数据操作特点
- 何时选择使用
mogodb简介
- MongoDB是一个开源、高性能、无模式的文档型数据库,当初的设计就是用于简化开发和方便扩展,是NoSQL数据库产品中的一种。是最像关系型数据库(MySQL)的非关系型数据库。
- 它支持的数据结构非常松散,是一种类似于JSON的格式叫BSON,所以它既可以存储比较复杂的数据类型,又相当的灵活。
- MongoDB中的记录是一个文档,它是一个由字段和值对(field:value)组成的数据结构。MongoDB文档类似于JSON对象,即一个文档认为就是一个对象。字段的数据类型是字符型,它的值除了使用基本的一些类型外,还可以包括其他文档、普通数组和文档数组。
体系结构
- 数据类型
特点
mogodb安装
windows安装
- 启动
mongod -f 配置文件地址
- shell连接
mongo
linux安装mongodb
常用命令
- 集合的删除
db.comment.drop()
- 插入文档
// 插入单条文档
db.comment.insert({"aitcleid":"1000002","content":"今天天气真好","createdatetime":new Date(),"likenum":NumberInt(11),"nickname":"LUCI","userid":"1002","state":null}
)
// 批量插入
db.comment.insertMany([{"aitcleid":"1000002","content":"今天天气真好","createdatetime":new Date(),"likenum":NumberInt(11),"nickname":"LUCI","userid":"1002","state":null},{"aitcleid":"1000002","content":"今天天气真好","createdatetime":new Date(),"likenum":NumberInt(11),"nickname":"LUCI","userid":"1002","state":null},{"aitcleid":"1000002","content":"今天天气真好","createdatetime":new Date(),"likenum":NumberInt(11),"nickname":"LUCI","userid":"1002","state":null}])
- 查找文档
db.comment.find({},{"nickname":1,"_id":0}); // 第一个参数是查询的条件,第二个参数是显示的列数,如果值为1,则显示,为0 则不显示,_id是默认显示的
- 使用try catch语法进行插入多条记录过程中异常的捕获,mongodb中没有事务的概念,不会因为某条记录无法插入停止运行,会继续完成插入的动作,所以需要使用异常捕获机制进行操作
try{
db.comment.insertMany([{"aitcleid":"1000002","content":"今天天气真好","createdatetime":new Date(),"likenum":NumberInt(11),"nickname":"LUCI","userid":"1002","state":null},{"aitcleid":"1000002","content":"今天天气真好","createdatetime":new Date(),"likenum":NumberInt(11),"nickname":"LUCI","userid":"1002","state":null},{"aitcleid":"1000002","content":"今天天气真好","createdatetime":new Date(),"likenum":NumberInt(11),"nickname":"LUCI","userid":"1002","state":null}])
}catch(e){
print(e)
}
- 文档的修改
- 局部修改,需要添加$set 修改器,否则是全局修改
db.comment.update({"userid":"1003"},{$set:{"likenum":NumberInt(1020)}});
- update默认是修改符合条件的第一条数据,如果全局修改,需要添加{multi:true}参数
db.comment.update({"userid":"1002"},{$set:{"likenum":NumberInt(10000)}},{multi:true});
- 更新一次,加指定的数据,添加$inc
db.comment.update({"userid":"1002"},{$inc:{"likenum":NumberInt(1)}});
- 文档的删除,如果在remove中不加任何条件,删除该集合中的所有记录,慎重使用
db.comment.remove({"userid":"1003"});
- 文档的分页查询
// 每页两个,skip中(页数-1)*每页显示的条数
db.comment.find().skip(2*2).limit(2);
- 文档的排列显示,1是降序,-1是升序
db.comment.find().sort({"userid":1});
- 文档的查询
- 比较查询
db.comment.find({"likenum":{$lt:2000}}) // lt小于
db.comment.find({"likenum":{$gt:2000}}) // 大于
db.comment.find({"likenum":{$ne:2000}}) //不等于
- 正则查询
db.comment.find({"content":/不好/}); // // 之间写正则表达式
- 包含查询 包含使用** i n ∗ ∗ 操作符。示例:查询评论的集合中 u s e r i d 字段包含 1003 或 1004 的文档,不包含使用 ∗ ∗ in**操作符。 示例:查询评论的集合中userid字段包含1003或1004的文档,不包含使用** in∗∗操作符。示例:查询评论的集合中userid字段包含1003或1004的文档,不包含使用∗∗nin**操作符。 示例:查询评论集合中userid字段不包含1003和1004的文档
db.comment.find({"userid":{$in:["1002","1003","1007"]}});
- 3.6.4 条件连接查询
我们如果需要查询同时满足两个以上条件,需要使用**$and**操作符将条件进行关联。(相 当于SQL的and) 格式为:
如果两个以上条件之间是或者的关系,我们使用**$or**操作符进行关联,与前面 and的使用方式相同 格式为:
- 常用命令总结
索引的操作
索引分为单级索引与多集索引
基本操作
- 查询索引
db.comment.getIndexes();
- 创建索引,1代表降序索引,-1代表升序索引
db.commnet.createIndex({"userid":1}); -
- 删除索引
db.comment.dropIndex({"userid":1}); // 以条件删除索引,以名称删除索引
执行计划
db.comment.find({},{"userid":1}).explain();
涵盖的查询
文章评论案例
- 引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
- 创建dao/mapper层
package com.lmx.mapper;
import com.lmx.pojo.Comment;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.mongodb.repository.cdi.MongoRepositoryBean;
public interface CommentMapper extends MongoRepository<Comment,String> {
Page<Comment> findByParentid(String parentid, Pageable pageable);
Page<Comment> findByAitcleid(String articleid,Pageable pageable);
}
- 实现业务方法,插入评论,根据文章id分页查询评论,根据父id查询子评论,点赞数+1,删除评论
package com.lmx.service;
import com.lmx.mapper.CommentMapper;
import com.lmx.pojo.Comment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.data.mongodb.core.query.UpdateDefinition;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Optional;
@Service
public class CommentService {
@Autowired
private CommentMapper commentMapper;
@Autowired
private MongoTemplate mongoTemplate;
/*
* 保存评论
* */
public void saveComment(Comment comment) {
commentMapper.save(comment);
// return true;
}
/*
实现删除评论
*/
public void RemoveComment(String id) {
commentMapper.deleteById(id);
// return true;
}
/*
* 查询分页查询评论
* */
public Page<Comment> GetAllComment(String articleid, int current, int size) {
return commentMapper.findByAitcleid(articleid,PageRequest.of(current,size));
}
/*
*
* 根据id查询评论
* */
public Comment GetCommentByid(String id) {
Optional<Comment> byId = commentMapper.findById(id);
return byId.get();
}
/*
* 根据上级id查询
* */
// curent大于0,从第一页开始
public Page<Comment> GetParentId(String parentid, int curent, int size) {
Page<Comment> byParentid = commentMapper.findByParentid(parentid, PageRequest.of(curent - 1, size));
return byParentid;
}
/*
* 评论点赞的业务
* */
public void AddLikeNum(String id) {
Query query = new Query(Criteria.where("_id").is(id));
Update update = new Update();
update.inc("likenum",1);
mongoTemplate.updateFirst(query,update,Comment.class);
}
}
mongodb的集群与安全