mongotemplate

SpringBoot整合MongoDB(一)

喜夏-厌秋 提交于 2020-02-17 09:05:30
SpringBoot整合MongoDB 此文章建立在Mongodb数据库已安装完成情况下。若服务器(linux系统/redhat)没有安装MongoDB,可查看我的另一篇文章 Centos7 使用Yum源安装MongoDB4.2版本数据库(补:密码配置) 本文章仅仅为单数据源配置,多数据源配置我在下一文中会编写。 本文章使用MongoTemplate而非springdatamongodb(个人感觉使用起来更舒适) 一.准备 创建SpringBoot工程,添加吐下依赖 可在创建工程时nosql那栏选择。 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> 二.文件配置 (1)YML文件配置 个人习惯使用yml格式对Springboot工程进行配置 mongodb连接配置如下: 无密码连接 spring: data: mongodb: uri: mongodb://服务器IP:端口/数据库名 有密码连接 spring: data: mongodb: uri: mongodb://用户名:密码@服务器IP:端口/数据库名 mongodb数据库与mysql不一样 mysql 一个普通用户可以管理多个数据库

spring boot mongo多数据源配置

允我心安 提交于 2020-01-25 07:45:03
配置文件 ## 第一个配置 spring . data . mongodb . first . database = idisback spring . data . mongodb . first . uri = mongodb : / / yingfuroot : yingfurootpwd@ 192.168 .104 .40 : 27017 / admin spring . data . mongodb . firs . username = yingfuroot spring . data . mongodb . firs . password = yingfurootpwd spring . data . mongodb . firs . authenticationDatabase = admin ## 第二个配置 spring . data . mongodb . second . database = handle spring . data . mongodb . second . uri = 192.168 .104 .39 : 27017 @Configuration public class MultipleMongoProperties { @ Bean ( name = "firstMongoProperties" ) @Primary @

【java】Spring+Mongodb,按月查询集合查询数据

北慕城南 提交于 2020-01-22 16:09:06
Spring+Mongodb在查询数据时,是可以指定查询集合的。那么现在的需求是按月份查询数据。 一、在serviceImpl上,先指定构造时间集合字符串 @Resource private MongoTemplate mongoTemplate ; private String monthCollection = String . format ( "%s_warningInfo" , formatDate ( new Date ( ) , "yyyyMM" ) ) ; * * 下面所有查询语句,指定集合时,都用的 monthCollection 。 二、按id查询,指定集合 @Override public WarningInfo loadById ( String id ) { WarningInfo warningInfo = mongoTemplate . findById ( id , WarningInfo . class , monthCollection ) ; return warningInfo ; } 三、保存一条记录,指定集合 @Override public int save ( WarningInfo record ) { record . setCreateTime ( new Date ( ) ) ; record . setUpdateTime

SpringBoot 系列教程(九十):Spring Boot配置使用MongoTemplate操作MongoDB

喜欢而已 提交于 2020-01-17 07:09:08
一、前言 在上一篇 SpringBoot 系列教程(七十):SpringBoot整合MongoDB 中我们详细的了解和学习到了 MongoDB 是什么、 MongoDB 能做什么、特点是啥、以及在 SpringBoot 中如何快速整合 MongoDB 数据库,并且进行了实战学习,如果有留意会发现在上一篇中是使用继承 MongoRepository 方式操作 MongoDB 增删改查,因为 MongoDB 同 JPA 一样, SpringBoot 同样为开发者准备了一套 Repository ,只需要继承 MongoRepository 传入实体类型以及主键类型即可。那么这篇文章我将带着大家学习以 MongoTemplate 方式来操作 MongoDB 数据库增删改查方法; 二、MongoTemplate是什么 MongoTemplate 是数据库和代码之间的接口,对数据库的操作都在它里面。 1. MongoTemplate 实现了interface MongoOperations。 2. MongoDB documents 和domain classes之间的映射关系是通过实现了 MongoConverter 这个 interface 的类来实现的。 3. MongoTemplate 提供了非常多的操作 MongoDB 的方法。 它是线程安全的,可以在多线程的情况下使用 来源:

MongoDB 多数据源动态配置

半城伤御伤魂 提交于 2019-12-28 05:04:15
文章目录 一、前言 二、简介 三、实现 1. MongoDBFactory 2. MongoDBConfig 3. 禁用mongodb的自动配置 4. MongoDB 的自动注入 4.1 MongoAutoConfiguration 4.2 MongoDataAutoConfiguration 5. 测试 一、前言 项目需要,单一MongoDB实例、多数据源配置。而百度发现,大部分都是通过声明多个Template实例或者其他类似的方式实现。但这种方式无法进行扩展(总不能添加一个数据源就再创建一个Template实例吧)。所以决定自己写一写。 二、简介 MongoDB是一个基于分布式文件存储 [1] 的数据库。由C++语言编写。旨在为WEB应用提供可扩展的高性能数据存储解决方案。MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。它支持的数据结构非常松散,是类似json的bson格式,因此可以存储比较复杂的数据类型。Mongo最大的特点是它支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引。 需要注意的是 : 当保存的文件大于16M时,需要使用 GridFsTemplate 来保存( GridFsTemplate 本质上使用的还是和

Spring MongoTemplate Update Merge

杀马特。学长 韩版系。学妹 提交于 2019-12-25 09:46:10
问题 Using spring's mongoTemplate or otherwise how do I perform a simple merge on an existing mongo document? When I say merge I want the following to happen: If a field in modifier document exists on the backend version of the document, then update it with the new value in the modifier. If a field in the modifier document does NOT exist on the backend version, then add the field. Leave all other fields on the Backend document alone. 回答1: If you wanna perform a merge using MongoTemplate you can do

Spring Mongo Template not saving the list of custom objects to MongoDb

主宰稳场 提交于 2019-12-25 03:11:23
问题 I am using spring Mongo Template to persist data to MongoDb. I have a custom object which has a list. Public Class CustomObject implements Serializable{ private CustomType1 header; private List<CustomType2> Values; } I created a wrapper class in order to wrap my custom object and the wrapper class looks like this public Class Wrapper { private String id; private Object object; } I am calling the save method like below Wrapper wrapper = new wrapper(key, value); mongoTemplate.save(wrapper,

Spring MongoTemplate - find by regex in collection

为君一笑 提交于 2019-12-23 01:41:43
问题 Saying i have a multivalue field in a mongo document: public class MongoEntity{ private List<String> field; } What would be the correct criteria for querying a regex in that field? I've tried Criteria.where(searchText).regex(searchText).in("field"); But that results in org.springframework.data.mongodb.UncategorizedMongoDbException: Can't canonicalize query: BadValue unknown top level operator: $in; nested exception is com.mongodb.MongoException: Can't canonicalize query: BadValue unknown top

Spring MongoTemplate - find by regex in collection

∥☆過路亽.° 提交于 2019-12-23 01:41:18
问题 Saying i have a multivalue field in a mongo document: public class MongoEntity{ private List<String> field; } What would be the correct criteria for querying a regex in that field? I've tried Criteria.where(searchText).regex(searchText).in("field"); But that results in org.springframework.data.mongodb.UncategorizedMongoDbException: Can't canonicalize query: BadValue unknown top level operator: $in; nested exception is com.mongodb.MongoException: Can't canonicalize query: BadValue unknown top

How to autowire mongoTemplate into custom type converter?

穿精又带淫゛_ 提交于 2019-12-22 10:49:04
问题 I'm trying to create a converter that will fetch object from DB by it's ObjectId. But the mongoTemplate is always empty in converter: org.springframework.core.convert.ConversionFailedException: Failed to convert from type org.bson.types.ObjectId to type com.atlas.mymodule.datadomain.MyObject for value '130000000000000000000013'; nested exception is java.lang.NullPointerException Code: @Component public class ObjectIdToMyObjectConverter implements Converter<ObjectId, MyObject> { @Autowired