SpringBoot中对MongoDB的基本操作是怎样的
发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,本篇文章为大家展示了SpringBoot中对MongoDB的基本操作是怎样的,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。SpringBoot 中对 Mong
千家信息网最后更新 2026年11月01日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) { //获取集合 MongoCollection collection = 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) { MongoCollection collection = 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) { MongoCollection collection = 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 List findDocuments() { 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) { MongoCollection collection = mongoTemplate.getCollection(COLLECTION_DEMO); DeleteResult result = collection.deleteOne(new BasicDBObject("name", name)); return result; } 上述内容就是SpringBoot中对MongoDB的基本操作是怎样的,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。
数据
基本操作
参数
内容
技能
知识
查询
简明
成功
简明扼要
业务
场景
大部分
字段
字符
字符串
字节
客户
客户端
就是
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网高科技
上海喔噻互联网科技有限公司
互联网科技公司简介
西安纸贵互联网科技有限公司
互联网科技有限公司图片
科技信息互联网
融势互联网科技
上游互联网科技
互联网科技咨询
互联网和高科技公司
广州优美互联网科技
恒银科技与互联网金融
互联网数据科技专业
区块链结合互联网科技
无锡工业互联网科技有限公司
上海京穆互联网科技有限公司
高科技互联网套路
中国互联网有什么科技
科技和互联网行业的特点
陕西彩京云互联网科技有限公司
互联网科技行业发展
互联网科技财经
科技创新互联网企业
科技大学互联网专业好不好
老年人脱离互联网科技的作文
智腾互联网科技有限公司
乌苏新华互联网科技做什么的
天津腾达互联网科技有限公司
互联网科技和法律行业
安徽零酒互联网科技有限公司