当前位置: 首页 > news >正文

mongodb:环境搭建

mongodb 是什么?

MongoDB是一款为web应用程序和互联网基础设施设计的数据库管理系统。没错MongoDB就是数据库,是NoSQL类型的数据库

为什么要用mongodb?

(1)MongoDB提出的是文档、集合的概念,使用BSON(类JSON)作为其数据模型结构,其结构是面向对象的而不是二维表,存储一个用户在MongoDB中是这样子的。

{username:'123',password:'123'
}

使用这样的数据模型,使得MongoDB能在生产环境中提供高读写的能力,吞吐量较于mysql等SQL数据库大大增强。

(2)易伸缩,自动故障转移。易伸缩指的是提供了分片能力,能对数据集进行分片,数据的存储压力分摊给多台服务器。自动故障转移是副本集的概念,MongoDB能检测主节点是否存活,当失活时能自动提升从节点为主节点,达到故障转移。

(3)数据模型因为是面向对象的,所以可以表示丰富的、有层级的数据结构,比如博客系统中能把“评论”直接怼到“文章“的文档中,而不必像myqsl一样创建三张表来描述这样的关系。

 

1、安装mongodb

 拉取镜像

docker pull mongo

创建容器

docker run -di --name mongo-service --restart=always -p 27017:27017 -v ~/data/mongodata:/data mongo

2、基本使用

①在项目中创建mongo-demo工程

        导入mongo依赖

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>

②mogo配置

server:port: 9998
spring:data:mongodb:host: 192.168.200.130port: 27017database: leadnews-history

③创建实体类

package com.itheima.mongo.pojo;import lombok.Data;
import org.springframework.data.mongodb.core.mapping.Document;import java.io.Serializable;
import java.util.Date;/*** <p>* 联想词表* </p>** @author itheima*/
@Data
@Document("ap_associate_words")
public class ApAssociateWords implements Serializable {private static final long serialVersionUID = 1L;private String id;/*** 联想词*/private String associateWords;/*** 创建时间*/private Date createdTime;}

④创建test测试类

        下面是基本使用

package com.itheima.mongo.test;import com.itheima.mongo.MongoApplication;
import com.itheima.mongo.pojo.ApAssociateWords;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Sort;
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.test.context.junit4.SpringRunner;import java.util.Date;
import java.util.List;@SpringBootTest(classes = MongoApplication.class)
@RunWith(SpringRunner.class)
public class MongoTest {@Autowiredprivate MongoTemplate mongoTemplate;//保存@Testpublic void saveTest(){ApAssociateWords apAssociateWords = new ApAssociateWords();apAssociateWords.setAssociateWords("头条");apAssociateWords.setCreatedTime(new Date());mongoTemplate.save(apAssociateWords);}//查询一个@Testpublic void saveFindOne(){ApAssociateWords apAssociateWords = mongoTemplate.findById("5fc2fc3fb60c9a039c44556e", ApAssociateWords.class);System.out.println(apAssociateWords);}//条件查询@Testpublic void testQuery(){Query query = Query.query(Criteria.where("associateWords").is("黑马头条")).with(Sort.by(Sort.Direction.DESC,"createdTime"));List<ApAssociateWords> apAssociateWordsList = mongoTemplate.find(query, ApAssociateWords.class);System.out.println(apAssociateWordsList);}@Testpublic void testDel(){mongoTemplate.remove(Query.query(Criteria.where("associateWords").is("黑马头条")),ApAssociateWords.class);}
}

http://www.lryc.cn/news/117900.html

相关文章:

  • Grafana技术文档--基本安装-docker安装并挂载数据卷-《十分钟搭建》
  • 【Github】Uptime Kuma:自托管监控工具的完美选择
  • linux环形缓冲区kfifo实践3:IO多路复用poll和select
  • SpringBoot系列---【使用jasypt把配置文件密码加密】
  • 大数计算(大数加法/大数乘法)
  • 【腾讯云 Cloud Studio 实战训练营】基于Cloud Studio构建React完成点餐H5页面
  • 杭电多校 Rikka with Square Numbers 费马平方和定理
  • 跟禹神VUE——组件间的通信方式(props配置项、组件间自定义事件、全局事件总线、消息订阅与发布、VUEX)
  • 《2023年中国企业数字化转型发展白皮书》发布
  • 基于Python 简易实现接口测试自动化
  • 创建线程、线程的挂起与恢复、线程的优先级与终止线程
  • [保研/考研机试] KY180 堆栈的使用 吉林大学复试上机题 C++实现
  • 【AI理论学习】手把手推导扩散模型:Diffusion Models(DDPM)
  • 智能汽车 论坛收集
  • 24届近5年南京航空航天大学自动化考研院校分析
  • Linux Day07
  • 数字化管理,让MRO工业品更高效
  • layui中渲染table表格
  • 2023-08-10LeetCode每日一题(下降路径最小和 II)
  • 网络基础2(HTTP,HTTPS,传输层协议详解)
  • Java实现籍贯级联选择器
  • 每日一学——OSI参考模型
  • 虚幻5中Lumen提供哪些功能以及如何工作的
  • Linux C 语言 mosquitto 方式 MQTT 发布消息
  • 利用NtDuplicateObject进行Dump
  • 【快应用】list组件如何区分滑动的方向?
  • 【深入了解pytorch】PyTorch扩展:如何使用PyTorch的扩展功能
  • Vue3——如何实现页面访问拦截
  • nginx配置gzip
  • ExtJS教程_编程入门自学教程_菜鸟教程-免费教程分享