赞
踩
目录
3.1 新建一个mongodb 文件夹,接着在里面新建一个date 文件夹
网站数据存储:
实时应用:MongoDB 非常适合需要频繁插入、更新和查询的实时应用程序,比如新闻feed、博客、论坛、评论系统等,其快速的写入速度和高效的查询性能有利于应对高并发访问。
游戏开发:
游戏用户信息:存储玩家账户、角色属性、装备、积分等数据,内嵌文档结构能很好地满足这类复杂且动态变化的数据需求。
实时数据分析:游戏事件日志、实时排行榜等场景要求数据库具备快速写入和即时查询的能力。
物流与电商:
订单管理:订单信息、商品库存、交易历史等,MongoDB 对频繁更新的状态跟踪表现优秀。
用户行为分析:记录并分析用户浏览、购买、搜索等行为数据。
社交网络:
用户资料与社交关系:存储用户个人信息、好友列表、消息记录等半结构化数据。
地理位置服务:利用地理空间索引轻松实现附近的用户、地点查找功能。
物联网(IoT):
设备数据存储:收集来自各种智能设备的实时或周期性上报的数据,如温度、湿度、状态变化等信息。
日志记录与分析:处理大量的设备日志数据,进行多维度分析和实时监控。
内容管理系统:
博客文章、多媒体内容存储:支持大文本、富媒体类型的内容存储,同时方便实现内容标签、分类等关联查询。
视频直播和流媒体:
用户活动记录:存储用户观看历史、互动行为(如送礼、弹幕)等信息。
实时统计与计费:对用户活动数据进行实时统计和计费计算。
缓存系统:
高性能缓存:作为高速缓存层,存储经常访问但不需永久保存或可以容忍短时间丢失的数据。
大数据分析:
聚合框架:MongoDB 内置了强大的聚合管道功能,可以在数据库层面完成数据预处理和初步分析。
docker run --restart=always -d --name mongo -v /opt/mongodb/data:/data/db -p 27017:27017 mongo:4.0.6
绿色解压即用
- 链接:https://pan.baidu.com/s/1WhEkYAwMD4g_IXQkmdXQIw?pwd=j6sb
- 提取码:j6sb
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-mongodb</artifactId>
- </dependency>
- # MongoDB连接信息
- spring.data.mongodb.host = 192.168.23.27
- spring.data.mongodb.port = 27017
- spring.data.mongodb.database = mall
- spring.data.mongodb.auto-index-creation = true
-
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- @Builder
- /**
- * 指定集合的名称如果不指定
- * 默认为当前类名小写
- */
- @Document(collection = "person")
- public class Person {
- @Id
- private Long id;
- private String name;
- private Integer age;
- /**
- * 创建一个10秒自动删除的文档
- * 一般结合spring.data.mongodb.auto-index-creation = true 使用
- * 注意:这个字段必须是data类型 或者是一个包含data类型的数组字段
- */
- // @Indexed(expireAfterSeconds = 30)
- // private LocalDateTime createTime;
- }

- package com.by;
-
- import cn.hutool.core.date.LocalDateTimeUtil;
- import com.by.model.Person;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.data.mongodb.core.MongoTemplate;
-
- import java.util.ArrayList;
- import java.util.List;
-
- @SpringBootTest
- class InsertTests {
- @Autowired
- private MongoTemplate mongoTemplate;
-
- /**
- * 插入操作
- * insert表示插入
- * 如果重复插入就报错
- */
- @Test
- void m1() {
- Person person = Person.builder().name("张三").age(18).id(1L).build();
- mongoTemplate.insert(person);
- }
-
- /**
- * Sava表示如果有就修改 如果没有就插入
- */
- @Test
- void m2() {
- Person person = Person.builder().name("张三1").age(18).id(1L).build();
- mongoTemplate.save(person);
- }
-
- /**
- * 自定义集合
- * 插入文档
- * 相当于将person 这个集合复制到person111里
- */
- @Test
- void m3() {
- Person person = Person.builder().name("张三1").age(18).id(1L).build();
- mongoTemplate.insert(person, "person111");
- }
-
- /**
- * 批量插入
- */
- @Test
- void m4() {
- List<Person> persons = new ArrayList<>();
- for (int i = 1; i <= 5; i++) {
- Person person = Person.builder().name("张三"+i).age(i).id(Long.valueOf(i)).build();
- persons.add(person);
- }
- mongoTemplate.insertAll(persons);
- }
-
- }

- package com.by;
-
- import com.by.model.Person;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- 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 java.util.ArrayList;
- import java.util.List;
-
- @SpringBootTest
- class UpdateTests {
- @Autowired
- private MongoTemplate mongoTemplate;
- /**
- * 修改数据
- * 方法一
- */
- @Test
- void m1()
- {
- Person per = mongoTemplate.findById(1L, Person.class);
- per.setName("武松");
- mongoTemplate.save(per);
- System.out.println("111");
- }
- /**
- * 修改数据
- * 方法二
- */
- @Test
- void m2()
- {
- Person per = mongoTemplate.findById(1L, Person.class);
- // 查询条件 查询id为1l
- Query query = new Query(Criteria.where("id").is(1L));
- // 修改内容
- Update update = new Update().set("name","宋江");
- mongoTemplate.updateFirst(query,update,Person.class);
- }
- /**
- * 批量删除数据
- */
- @Test
- void m3()
- {
- // 查询条件 gte表示大于等于
- Query query = new Query(Criteria.where("id").gte(1L));
- // 修改内容
- Update update = new Update().set("name","宋江");
- mongoTemplate.updateMulti(query,update,Person.class);
- }
- }

- package com.by;
-
- import com.by.model.Person;
- import com.mongodb.client.result.DeleteResult;
- import org.junit.jupiter.api.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- 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 java.util.List;
-
- @SpringBootTest
- class DeleteTests {
- @Autowired
- private MongoTemplate mongoTemplate;
-
- /**
- * 删除id 为1的记录
- */
- @Test
- void m1() {
- Query query = new Query(Criteria.where("id").is(1L));
- DeleteResult remove = mongoTemplate.remove(query, Person.class);
- System.out.println("删除的条数为:" + remove.getDeletedCount());//1
-
- }
- /**
- * 删除符合条件的所有文档
- */
- @Test
- public void m2() throws Exception {
- //删除年龄小于18的所有人: lt 表示小于等于
- Query query = new Query(Criteria.where("age").lt(18));
- DeleteResult result = mongoTemplate.remove(query, Person.class);
- System.out.println("删除条数:" + result.getDeletedCount());
- }
- /**
- * 删除符合条件的单个文档 并返回删除的文档
- */
- @Test
- public void m3() throws Exception {
- Query query = new Query(Criteria.where("id").is(2L));
- Person per = mongoTemplate.findAndRemove(query, Person.class);
- System.out.println(per);//Person(id=2, name=宋江, age=2)
- }
- /**
- * 删除符合条件的所有文档
- * 并返回删除的所有文档
- */
- @Test
- public void m4(){
- // lt 表示小于
- Query query = new Query(Criteria.where("age").lt(3));
- List<Person> pers = mongoTemplate.findAllAndRemove(query, Person.class);
- System.out.println(pers);
- }
- }

- package com.by;
-
- import com.by.model.Person;
- import org.junit.jupiter.api.Test;
- 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.data.mongodb.core.query.Update;
-
- import java.util.Arrays;
- import java.util.List;
-
- @SpringBootTest
- class QueryTests {
- @Autowired
- private MongoTemplate mongoTemplate;
-
- /**
- * 查询集合中的全部文档数据
- */
- @Test
- public void findAll() {
- List<Person> personList = mongoTemplate.findAll(Person.class);
- System.out.println("查询结果:" + personList.toString());
- }
- /**
- * 查询集合中指定的ID文档数据
- */
- @Test
- public void findById() {
- Person byId = mongoTemplate.findById(3L, Person.class);
- System.out.println("查询结果:" + byId.toString());
- }
-
- /**
- * 根据条件查询符合条件的文档数据并返回第一条数据
- */
- @Test
- public void findOne() {
- Query query = new Query(Criteria.where("name").is("宋江1"));
- Person result = mongoTemplate.findOne(query, Person.class);
- System.out.println("查询结果:" + result.toString());
- }
-
- /**
- * 根据条件查询所有符合条件的文档
- */
- @Test
- public void findByCondition() {
-
- Query query = new Query(Criteria.where("age").gt(18));
- List<Person> result = mongoTemplate.find(query, Person.class);
- System.out.println("查询结果:" + result.toString());
- }
-
- /**
- * 根据【AND】关联多个查询条件,查询集合中所有符合条件的文档数据
- */
- @Test
- public void findByAndCondition() {
- // 创建条件
- Criteria name = Criteria.where("name").is("张三");
- Criteria age = Criteria.where("age").is(18);
- // 创建条件对象,将上面条件进行 AND 关联
- Criteria criteria = new Criteria().andOperator(name, age);
- // 创建查询对象,然后将条件对象添加到其中
- Query query = new Query(criteria);
- List<Person> result = mongoTemplate.find(query, Person.class);
- System.out.println("查询结果:" + result.toString());
- }
-
- /**
- * 根据【OR】关联多个查询条件,查询集合中的文档数据
- */
- @Test
- public void findByOrCondition() {
- // 创建条件
- Criteria criteriaUserName = Criteria.where("name").is("张三");
- Criteria criteriaPassWord = Criteria.where("age").is(22);
- // 创建条件对象,将上面条件进行 OR 关联
- Criteria criteria = new Criteria().orOperator(criteriaUserName, criteriaPassWord);
- // 创建查询对象,然后将条件对象添加到其中
- Query query = new Query(criteria);
- List<Person> result = mongoTemplate.find(query, Person.class);
- System.out.println("查询结果:" + result.toString());
- }
-
- /**
- * 根据【IN】关联多个查询条件,查询集合中的文档数据
- */
- @Test
- public void findByInCondition() {
- // 设置查询条件参数
- List<Long> ids = Arrays.asList(10L, 11L, 12L);
- // 创建条件
- Criteria criteria = Criteria.where("id").in(ids);
- // 创建查询对象,然后将条件对象添加到其中
- Query query = new Query(criteria);
- List<Person> result = mongoTemplate.find(query, Person.class);
- System.out.println("查询结果:" + result.toString());
- }
-
- /**
- * 根据【逻辑运算符】查询集合中的文档数据
- */
- @Test
- public void findByOperator() {
- // 设置查询条件参数
- int min = 20;
- int max = 35;
- Criteria criteria = Criteria.where("age").gt(min).lte(max);
- // 创建查询对象,然后将条件对象添加到其中
- Query query = new Query(criteria);
- List<Person> result = mongoTemplate.find(query, Person.class);
- System.out.println("查询结果:" + result.toString());
- }
-
- /**
- * 根据【正则表达式】查询集合中的文档数据
- */
- @Test
- public void findByRegex() {
- // 设置查询条件参数
- String regex = "^张";
- Criteria criteria = Criteria.where("name").regex(regex);
- // 创建查询对象,然后将条件对象添加到其中
- Query query = new Query(criteria);
- List<Person> result = mongoTemplate.find(query, Person.class);
- System.out.println("查询结果:" + result.toString());
- }
-
- /**
- * 根据条件查询集合中符合条件的文档,获取其文档列表并排序
- */
- @Test
- public void findByConditionAndSort() {
- Query query = new Query(Criteria.where("name").is("张三")).with(Sort.by("age"));
- List<Person> result = mongoTemplate.find(query, Person.class);
- System.out.println("查询结果:" + result.toString());
- }
-
- /**
- * 根据单个条件查询集合中的文档数据,并按指定字段进行排序与限制指定数目
- */
- @Test
- public void findByConditionAndSortLimit() {
- String userName = "张三";
- //从第5行开始,查询3条数据返回
- Query query = new Query(Criteria.where("name").is("张三"))
- .with(Sort.by("createTime"))
- .limit(3).skip(5);
- List<Person> result = mongoTemplate.find(query, Person.class);
- System.out.println("查询结果:" + result.toString());
- }
-
- /**
- * 统计集合中符合【查询条件】的文档【数量】
- */
- @Test
- public void countNumber() {
- // 设置查询条件参数
- String regex = "^张*";
- Criteria criteria = Criteria.where("name").regex(regex);
- // 创建查询对象,然后将条件对象添加到其中
- Query query = new Query(criteria);
- long count = mongoTemplate.count(query, Person.class);
- System.out.println("统计结果:" + count);
- }
-
- }

-
- package com.by;
-
- import com.mongodb.client.ListIndexesIterable;
- import com.mongodb.client.model.Indexes;
- import org.junit.Test;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.boot.test.context.SpringBootTest;
- import org.springframework.data.mongodb.core.MongoTemplate;
- import org.springframework.data.mongodb.core.mapping.Document;
-
- @SpringBootTest
- public class IndexTest {
- @Autowired
- private MongoTemplate mongoTemplate;
-
- /**
- * 创建升序索引
- */
- @Test
- public void createAscendingIndex() {
- // 设置字段名称
- String field = "age";
- // 创建索引
- mongoTemplate.getCollection("person").createIndex(Indexes.descending(field));
- }
-
- /**
- * 根据索引名称移除索引
- */
- @Test
- public void removeIndex() {
- // 设置字段名称
- String field = "age_1";
- // 删除索引
- mongoTemplate.getCollection("person").dropIndex(field);
- }
-
-
- /**
- * 查询集合中所有的索引
- */
- // @Test
- // public void getIndexAll() {
- // // 获取集合中所有列表
- // ListIndexesIterable<org.bson.Document> indexList = mongoTemplate.getCollection("person").listIndexes();
- // // 获取集合中全部索引信息
- // for (Document document : indexList) {
- // System.out.println("索引列表:" + document);
- // }
- // }
- }

https://codeup.aliyun.com/62858d45487c500c27f5aab5/huang-springboot-mongodb.git
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。