MangoDB

概述

MangoDB具有高性能、高存储、数据具有结构性的特点。

与Mysql相比,他的性能更高;与Redis相比,他的数据更有结构性

应用场景:数据量较大,要求加载数据快

环境配置

  1. 拉取镜像
sh 复制代码
docker pull mango
  1. 创建容器
sh 复制代码
docker run -di --name mongo-service --restart=always -p 27017:27017 -v /data/mongodata:/data mongo
  1. 导入依赖
xml 复制代码
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
  1. 配置文件
yml 复制代码
spring: 
	data:
		mongodb: 
			host: 192.168.133.128
			port: 27017
			database: DatabaseName
  1. 创建映射
    将实体映射到MangoDB的集合,类似将实体映射到Mysql的表
java 复制代码
@Document("ap_associate_words")
public class ApAssociatewords implements Serializable{
	private static final long serialVersionUID = 1L;
	
	private String id;
	/**
	 * 联想词
	 */
	private String associatewords;
	
	/**
	 * 创建时间
	 */
	private Date createdTime;
}
  1. 示例代码
java 复制代码
public class MangoDemo{
	@Autowired
	MangoTemplate mangoTemplate;

	public void test(){
		//保存或修改(根据ID,有ID就修改,无ID就保存)
		mangoTemplate.save(apAssociatewords);
		
		//根据ID查询集合得到实体
		ApAssociatewords apAssociatewords = mongoTemplate.findById("5fc2fc3fb60c9a039c44556e",ApAssociatewords.class);

		//根据条件去查询实体
		Query query = Query.query(Criteria.where("associatewords").is("测试字段")).with(Sort.by(Sort.Direction.DESC,"createdTime"));
		List<ApAssociatewords apAssociatewordsList = mongoTemplate.find(query, ApAssociateWords.class);

		//根据条件删除文档
		mongoTemplate.remove(Query.query(Criteria.where("associatewords").is("测试字段")),ApAssociateMords.class);

	}
}
相关推荐
AI全栈实验室4 天前
MongoDB迁移金仓踩了5个坑,最后一个差点回滚
mongodb
数据知道5 天前
MongoDB 元素查询运算符:使用 `$exists` 检查字段是否存在及处理缺失字段
数据库·mongodb
数据知道5 天前
MongoDB 批量写操作:`bulkWrite()` 在数据迁移与清洗中的高性能应用
数据库·mongodb
数据知道5 天前
MongoDB 数组更新操作符:`$push`、`$pull`、`$addToSet` 管理列表数据
数据库·mongodb
数据知道5 天前
MongoDB 更新操作符 `$set` 与 `$unset`:精准修改字段与删除字段
数据库·mongodb
数据知道5 天前
MongoDB 数值更新原子操作:`$inc` 实现点赞、计数器等高并发原子操作
数据库·算法·mongodb
数据知道6 天前
MongoDB 数组查询专项:`$all`、`$elemMatch` 与精确匹配数组的使用场景
数据库·mongodb
数据知道6 天前
MongoDB 正则表达式查询:在 MongoDB 中实现模糊搜索与索引优化陷阱
数据库·mongodb·正则表达式
正在走向自律6 天前
文档数据库替换新范式:金仓数据库MongoDB兼容性深度解析与实践指南
数据库·mongodb·国产数据库·金仓数据库
I'mAlex7 天前
金仓数据库平替MongoDB实操解析:多模融合赋能企业文档数据管理国产化升级
数据库·mongodb·kingbasees·金仓数据库