沛县专业做网站,c 中怎么用html5做网站,wordpress顶部广告怎么添加,wordpress filtermongodb 是什么#xff1f;
MongoDB是一款为web应用程序和互联网基础设施设计的数据库管理系统。没错MongoDB就是数据库#xff0c;是NoSQL类型的数据库
为什么要用mongodb#xff1f;
#xff08;1#xff09;MongoDB提出的是文档、集合的概念#xff0c;使用BSON
MongoDB是一款为web应用程序和互联网基础设施设计的数据库管理系统。没错MongoDB就是数据库是NoSQL类型的数据库
为什么要用mongodb
1MongoDB提出的是文档、集合的概念使用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 --restartalways -p 27017:27017 -v ~/data/mongodata:/data mongo
2、基本使用
①在项目中创建mongo-demo工程 导入mongo依赖
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-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));ListApAssociateWords apAssociateWordsList mongoTemplate.find(query, ApAssociateWords.class);System.out.println(apAssociateWordsList);}Testpublic void testDel(){mongoTemplate.remove(Query.query(Criteria.where(associateWords).is(黑马头条)),ApAssociateWords.class);}
}