SpringBoot中对MongoDB的基本操作是怎样的
发表于:2025-02-02 作者:千家信息网编辑
千家信息网最后更新 2025年02月02日,本篇文章为大家展示了SpringBoot中对MongoDB的基本操作是怎样的,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。SpringBoot 中对 Mong
千家信息网最后更新 2025年02月02日SpringBoot中对MongoDB的基本操作是怎样的
本篇文章为大家展示了SpringBoot中对MongoDB的基本操作是怎样的,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
SpringBoot 中对 MongoDB的基本操作
Database 库的创建
首先 在MongoDB 操作客户端 Robo 3T 中 创建数据库:
增加用户User:
创建 Collections 集合(类似mysql 中的 表):
后面我们大部分都是基于创建的Collection "collectiondemo"操作的。
依赖包
org.springframework.boot spring-boot-starter-data-mongodb 2.1.7.RELEASE org.springframework.boot spring-boot-starter-web 2.1.7.RELEASE org.springframework.boot spring-boot-starter-test test 2.1.7.RELEASE
SpringBoot 中 application.properties 文件配置
spring.data.mongodb.host=localhostspring.data.mongodb.port=27017spring.data.mongodb.database=mongodemospring.data.mongodb.username=loginspring.data.mongodb.password=login
MongoDB 中的 增删改查 操作
定义 MongoTemplate
@Autowired private MongoTemplate mongoTemplate; private static String COLLECTION_DEMO = "collectiondemo";
基于 mongoTemplate 操作
添加数据
@PostMapping("/insertDocument") public void insertDocument(String document) { //获取集合 MongoCollectioncollection = mongoTemplate.getCollection(COLLECTION_DEMO); Document parse = Document.parse(document); //插入文档 collection.insertOne(parse); }
postman 测试参数:
在 Robo 中 可以查询到:
添加数据成功 ,其中 ObjectId是一个12字节的 BSON 类型字符串, 由
组成
插入数据
@PutMapping("/updateDocument") public Long updateDocument(String queryDocument, String ducument) { MongoCollectioncollection = mongoTemplate.getCollection(COLLECTION_DEMO); BasicDBObject queryParse = BasicDBObject.parse(queryDocument); BasicDBObject parse = BasicDBObject.parse(ducument); UpdateResult result = collection.updateOne(queryParse, new BasicDBObject("$set",parse)); return result.getModifiedCount(); }
输入参数:
可以看到:
但有个问题,当参数中 key 在 mongodb 不存在时,会自己创建:
之前mongodb并没有 age 字段,现在可以看到:
这可能对有些业务场景,对key要求严格的 就无法通过这个满足条件 ,此时mongodb 中 可以用$exists 解决:
@PutMapping("/updateDocumentOnlyHave") public Long updateDocumentOnlyHave(String id, String ducument) { MongoCollectioncollection = mongoTemplate.getCollection(COLLECTION_DEMO); BasicDBObject parse = BasicDBObject.parse(ducument); Set keySet = parse.keySet(); BasicDBObject dbObject = new BasicDBObject(); dbObject.put("id",id); for (String key : keySet) { dbObject.put(key, new BasicDBObject("$exists",true)); } UpdateResult result = collection.updateOne(dbObject, new BasicDBObject("$set",parse)); return result.getModifiedCount(); }
查询数据
@GetMapping("/listDocuments") public ListfindDocuments() { MongoCollection collection = mongoTemplate.getCollection(COLLECTION_DEMO); FindIterable documents = collection.find(); List listDocuments = new ArrayList<>(); for (Document document : documents) { listDocuments.add(document); } return listDocuments; }
删除数据
@DeleteMapping("/deleteDocument") public DeleteResult deleteDocument(String name) { MongoCollectioncollection = mongoTemplate.getCollection(COLLECTION_DEMO); DeleteResult result = collection.deleteOne(new BasicDBObject("name", name)); return result; }
上述内容就是SpringBoot中对MongoDB的基本操作是怎样的,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。
数据
基本操作
参数
内容
技能
知识
查询
简明
成功
简明扼要
业务
场景
大部分
字段
字符
字符串
字节
客户
客户端
就是
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
网络安全真实案例及分析2000字
移动网络技术岗发展前途
Dota2为什么一直协调服务器
普法考试网络安全法禁止
数据库结构变更的升级与回退
市住建局网络安全工作要求
越南服务器哪里便宜
国家网络安全周宣传册
服务器这么进安全模式
南京市网络技术有限公司地址
案例分析数据库知识
数据库如何创建双主键
软件开发成功的因素
信息网络安全包括哪些
linux ftp服务器配置
软件开发项目评审表模板
个性化软件开发标语
从化软件开发院校中专
移动软件开发工资待遇
艾提科信网络技术有限公司
计算机网络技术清华大学
腾讯软件开发师工资待遇
Bvj数据库
网络技术找工作
上海合发网络技术
安徽省全员人口数据库系统
网络安全伴我成长手抄报四年级
越南服务器哪里便宜
网络安全法 舆情
欧姆龙自动化软件开发