千家信息网

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) {                //获取集合                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的基本操作是怎样的,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注行业资讯频道。

0