spring-data-mongodb

MongoDB Java Driver creating Database and Collection

混江龙づ霸主 提交于 2019-12-05 18:41:08
i was testing how to create database and collection mongo java driver. MongoClient client = new MongoClient("localhost",27017); DB db = client.getDB("ow"); DBCollection collection = db.getCollection("documents"); collection.save(new BasicDBObject("_id",1)); collection.remove(new BasicDBObject("_id",1)); boolean result = db.collectionExists("documents"); assertTrue(result); assertNotNull(collection); client.close(); I would prefer to use createCollection method on the DB object, but found that it does not create database / collection unless the first document is inserted. My question is is this

Mongo spring-data issue with java.util.Currency

亡梦爱人 提交于 2019-12-05 18:27:13
Getting error "No property null found on entity class java.util.Currency" Hi I have Document Class as @Document @JsonInclude(Include.NON_NULL) public class Course { @Id private String id; private String title; private Currency curr; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public Currency getCurr() { return curr; } public void setCurr(Currency curr) { this.curr = curr; } } when in make POST request to my service with data {"title":"my title","curr":

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

Spring MongoDB + QueryDSL query by @DBRef related object

心不动则不痛 提交于 2019-12-05 16:51:16
I am using spring-data-mongodb and querydsl-mongodb to perform more flexible queries. My application has users and orders. An user can have multiple orders, so my models looks like this: public class User { @Id private String id; private String username; //getters and setters } public class Order { @Id private String id; @DBRef private User user; //getters and setters } As you can see, there is an has-many relationship between users and orders. Each order is assigned to an user, and the user is stored in @DBRef public User user attribute. Now, lets say that an user has 10,000 orders. How can i

How to modeling when use Spring data mongo and Spring data elasticearch?

泄露秘密 提交于 2019-12-05 14:50:28
I want to use mongo and ElasticSearch in my projects, and I also like adopt Spring Data Mongo and Spring Data ElasticSearch, but both has their Repository and model specs, how to use them together? There are some options: Use the same model class for Mongo and ElasticSearch? @Document//from Spring Data Mongo @Document// from Spring Data ElasticSearch public class Book{ @Id//Spring Data Commons private String id; } But there are some mismatch in Spring Data Mongo and Spring Data ElasticSearch, such as Geo field type. Define different model for Mongo and ElasticSearch, and copy data state from

spring-data-jpa + spring-data-mongodb 配置及jar冲突异常解

放肆的年华 提交于 2019-12-05 08:46:23
当同时集成 spring-data-jpa + spring-data-mongodb 时应注意 将其repository(DAO)层区分开来 我的做法 所有 以 *.dao 结尾的 统统视为 Spring data jpa 所有 以 *.mongo. repository 统统视为 spring-data-mongodb 原因: 兼容遗留代码 spring-data-mongo.xml 配置 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mongo="http://www.springframework.org/schema/data/mongo" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/data/mongo http:/

How exactly does spring-data-mongodb handle constructors when rehydrating objects?

纵然是瞬间 提交于 2019-12-05 06:36:49
I have read http://static.springsource.org/spring-data/data-mongo/docs/1.1.0.RELEASE/reference/html/#mapping-chapter but cannot find the answer to the following basic spring-data-mongodb object mapping question: If I load an instance of the following class from MongoDB: public class Test { private String str1; private String str2; private Date date3; public Test(String str1) { this.str1 = str1; this.date3=new Date(); } } I understand that the constructor Test(String str1) will be invoked with the value found in the top-level field str1 of the MongoDB document. I assume this constructor is

Register a CustomConverter in a MongoTemplate with Spring Boot

北战南征 提交于 2019-12-05 03:35:39
How can I register a custom converter in my MongoTemplate with Spring Boot? I would like to do this only using annotations if possible. Zubair Nabi You need to create a configuration class for converter config. @Configuration @EnableAutoConfiguration(exclude = { EmbeddedMongoAutoConfiguration.class }) @Profile("!testing") public class MongoConfig extends AbstractMongoConfiguration { @Value("${spring.data.mongodb.host}") //if it is stored in application.yml, else hard code it here private String host; @Value("${spring.data.mongodb.port}") private Integer port; @Override protected String

Spring Data MongoDB Date between two Dates

Deadly 提交于 2019-12-05 03:34:55
i'm using Spring Data for MongoDB and got the following classes class A { List<B> b; } class B { Date startDate; Date endDate; } when i save an object of A it gets persisted like { "_id" : "DQDVDE000VFP8E39", "b" : [ { "startDate" : ISODate("2009-10-05T22:00:00Z"), "endDate" : ISODate("2009-10-29T23:00:00Z") }, { "startDate" : ISODate("2009-11-01T23:00:00Z"), "endDate" : ISODate("2009-12-30T23:00:00Z") } ] } Now i want to query the db for documents matching entries in b where a given date is between startDate and endDate. Query query = new Query(Criteria.where("b").elemMatch( Criteria.where(

Spring Data Mongo - How to query by @DBRef field's id

♀尐吖头ヾ 提交于 2019-12-05 02:12:29
I am new to Spring Data Mongo so I must be doing something wrong because I can't manage to execute such a simple query. This is my model: @Document(collection="brands") public class Brand{ @Id private int id; private String name; ... //getters-setters } @Document(collection="models") public class Model{ @Id private int id; private String name; @DBRef private Brand brand; ... //getters-setters } I would like to get all models from a brand, so I implement the DAO as follows: @Repository public interface IModelDAO extends MongoRepository<Model, Integer>{ @Query(value="{ 'brand.$id' : ?0 }")