mongotemplate

MongoDB Projection on embedded document field with mongoTemplate

霸气de小男生 提交于 2019-12-11 15:22:57
问题 In my java application i want retrieve a field of an embedded document. This is my pojo: My User.class public class User implements Comparable<User> { @Id private String username; private String ownerFirstname; private String ownerLastname; @DBRef @CascadeSave @JsonInclude(JsonInclude.Include.NON_NULL) private Role role; } Role.class @Document @JsonDeserialize(using = RoleDeserializer.class) public interface Role { String getId(); void setId(String id); } Society.class @Document(collection =

Use multiple/different MongoTemplates with same Respository class

妖精的绣舞 提交于 2019-12-10 21:19:50
问题 I have a collection on three different mongo databases(same schema). So I have created a Repository for this. But is it possible that i can use different mongoTemplates(one for three different databases) but same repository class. I can create three different repository classes(one for each database), But I don't want to do this because the schema is the same on all three. Thanks 来源: https://stackoverflow.com/questions/38265818/use-multiple-different-mongotemplates-with-same-respository-class

MongoTemplate aggregate - group by date

孤街醉人 提交于 2019-12-07 20:13:08
问题 I'm trying to create an aggregate query using mongotemplate where there's a grouping by date (i.e 2016-03-01) instead of datetime (i.e 2016-03-01 16:40:12). The dateToString operation exists in the mongodb documentation it can be used to extract the date from the datetime using formatting: https://docs.mongodb.org/manual/reference/operator/aggregation/dateToString/ but I get get it to work with mongotemplate - I get a NullPointerException. (my db version is 3.2) List<AggregationOperation>

Spring MongoTemplate - find by regex in collection

耗尽温柔 提交于 2019-12-06 15:18: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 level operator: $in So after many trial and errors, turns out it's simpler (although a little counter

SpringBoot 整合mongoDB并自定义连接池

。_饼干妹妹 提交于 2019-12-06 15:16:21
SpringBoot 整合mongoDB并自定义连接池 得力于SpringBoot的特性,整合mongoDB是很容易的,我们整合mongoDB的目的就是想用它给我们提供的mongoTemplate,它可以很容易的操作mongoDB数据库。 为了自定义连接池,我们在配置类中主要与MongoClientOptions、MongoCredential、MongoClient、MongoDbFactory打交道。最终的目的就是配置好一个MongoDbFactory的bean交由Spring管理。 Maven 依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> 配置文件 mongodb: database: bfa_mongo username: "xxx" password: "xxxxx" address: "host:port" authenticationDatabase: [设置你的认证数据库,如果有的话] # 连接池配置 clientName: ${spring.application.name} # 客户端的标识,用于定位请求来源等

Spring Boot + MongoDB

白昼怎懂夜的黑 提交于 2019-12-06 02:48:31
项目结构 下面直接进入代码操作 创建一个Springboot项目,pom.xml引入MongoDB依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> 接下来配置MongoDB数据源,此处我用的是本地安装的MongoDB,所以没有用户名和密码,有用户名和密码的格式是: spring.data.mongodb.uri=mongodb://name:pass@localhost:27017/test,其中name是用户名,pass是密码 如果要配置多个数据库,则中间用 "," 分割,例如 spring.data.mongodb.uri=mongodb://192.168.1.1:20000,192.168.1.2:20000,192.168.252.12:20000/test application.properties: spring.data.mongodb.uri=mongodb://localhost:27017/mydb User.class package com.zyu.entity; /** * @program: SpringBoot-mongo *

How to autowire mongoTemplate into custom type converter?

不问归期 提交于 2019-12-05 18:22:37
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 private MongoTemplate mongoTemplate; // null ??? public MyObject convert(ObjectId objectId) { return

SpringBoot整合MongoDB

我怕爱的太早我们不能终老 提交于 2019-12-05 14:13:49
MongoDB MongoDB是 open-source NoSQL 文档数据库,它使用 JSON-like schema 而不是传统的 table-based 关系数据。 Spring Boot 提供了一些使用 MongoDB 的便利,包括 spring-boot-starter-data-mongodb 和 spring-boot-starter-data-mongodb-reactive “Starters”。 连接到 MongoDB 数据库 要访问 Mongo 数据库,可以 inject auto-configured org.springframework.data.mongodb.MongoDbFactory 。默认情况下,实例尝试在 mongodb://localhost/test 处连接到 MongoDB 服务器。以下 example 显示如何连接到 MongoDB 数据库: import org.springframework.data.mongodb.MongoDbFactory; import com.mongodb.DB; @Component public class MyBean { private final MongoDbFactory mongo; @Autowired public MyBean(MongoDbFactory mongo) {

MongoTemplate Criteria Query

不问归期 提交于 2019-12-04 13:29:42
问题 I'm generating a complicated Mongo query depending on multiple parameters. One of criterion that I want to make with Criteria helper class is: {"field1": {$exists: true, $ne: false}} I tried to make it with: Criteria.where("field1").is(Criteria.where("$ne").is(false).and("$exists").is(true)) But it generates: { "field1" : { $java : org.springframework.data.mongodb.core.query.Criteria@23864e60 } So, how to achieve the exact query that i need? I can't hardcode that query string, because these

SpringBoot整合MongoDB

耗尽温柔 提交于 2019-12-03 23:30:50
本节使用SpringBoot 2.1.9.RELEASE, 示例源码 在 https://github.com/laolunsi/spring-boot-examples/tree/master/06-spring-boot-mongo-demo SpringBoot可以非常方便地引入和操作MongoDB。本节分两部分,记录个人学习SpringBoot使用MongoDB数据库的一些知识。 第一部分是一个简单的springboot连接mongo的demo,测试查询功能。 第二部分是基于mongo实现的增删改查数据示例。 本节使用SpringBoot 2.1.9.RELEASE 一、A simple demo 首先来演示SpringBoot项目引入MongoDB,以及一个简单的findAll操作。 maven依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> 环境配置: application.yml文件中写入如下配置,test表示MongoDB中的test表 spring: data: mongodb: uri: "mongodb://localhost:27017