spring-data-mongodb

Resolving subdocument types with Spring Data and MongoDB

断了今生、忘了曾经 提交于 2019-12-22 10:57:12
问题 I'm encountering an error with a Spring Data repository as it attempts to resolve a property expression: public interface ContractRepository extends MongoRepository<Contract,String> { public List<Contract> findByCodeBindings(String binding); } Here's the relevant parts of Contract : @Document(collection="CONTRACTS") public class PersistentContract extends BaseContract { @PersistenceConstructor public PersistentContract(String name, Version version, Code code) { super(name, version, code); } }

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

Register a CustomConverter in a MongoTemplate with Spring Boot

人盡茶涼 提交于 2019-12-22 04:36:15
问题 How can I register a custom converter in my MongoTemplate with Spring Boot? I would like to do this only using annotations if possible. 回答1: 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

Spring data mongodb: mongodb repositories not found when autowired

杀马特。学长 韩版系。学妹 提交于 2019-12-21 22:30:23
问题 I have the following stack: Spring MVC web app Spring Data MongoDB When I deploy the webapp and start server (Tomcat 7) I'am getting the following error : org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.qopuir.repository.UserRepository com.qopuir.service.UserService.userRepository;

Using $cond operator with Spring-data-mongodb [duplicate]

限于喜欢 提交于 2019-12-21 21:36:01
问题 This question already has answers here : How to use $cond operation in Spring-MongoDb aggregation framework (2 answers) Closed last year . I am looking to aggregate the following data { "user": "user1", "error": true } { "user": "user2", "error": false } { "user": "user1", "error": false } Into { "_id": "user1", "errorCount": 1, "totalCount": 2 }, { "_id": "user2", "errorCount": 0, "totalCount": 1 } With $cond operator, this can be achieved using: $group: { _id: "$user", errorCount : { "$sum"

Spring data MongoDB adding arrays to an existing document

随声附和 提交于 2019-12-21 20:47:07
问题 Say I have the following Collections public @Data class Customer { @Id private String id; private String firstName; private String lastName; @DBRef private List<Address> addressList= new ArrayList<Address>(); } and public @Data class Address { @Id private String id; private String address; private String type; private String customerID; } And each Customer has multiple addresses, and I have implemented MongoRepository . Saving customer for the First time is working pretty well customerRepo

SpringData MongoDB Using projection

冷暖自知 提交于 2019-12-21 18:01:47
问题 Is their any way via which I can use mongodb projection feature to fetch selected fields from the document. Even after searching for whole day on internet not able to find any clue. 回答1: Here's the relevant section of the docs concerning repositories. Also the section on querying documents has a brief mention of it. When using MongoOperations you can use the Query instance to restrict the result to certain fields: Query query = Query.query(Criteria.where("lastname").eq("…")); // Configure the

Spring Data MongoDB: How ignore unique indexed field when Document is embedded in another one?

﹥>﹥吖頭↗ 提交于 2019-12-21 11:03:12
问题 I have a Contract class defined like this: @Document public class Contract { @Id private String id; @Indexed(unique = true) private String ref; private String status = "pending"; // getter & setter & hashcode & equals & tostring... } I want to save contract state over time, so I created a Version class like this: @Document public class Version { @Id private String id; private Contract contract; private Instant createdAt; // getter & setter & hashcode & equals & tostring... } When I try to

Java 8 stream support in MongoTemplate

送分小仙女□ 提交于 2019-12-21 09:35:21
问题 Why don't we have java 8 stream support in MongoTemplate in spring-data-mongodb-1.8.2.RELEASE.jar ? I see that stream support have bean added in MongoRepository interface but I use pure MongoTemplate. 回答1: In short There is stream support but without exposing Stream on MongoOperations . Explanation Spring Data Mongo has stream support by exposing CloseableIterator<T> stream(final Query query, final Class<T> entityType) . It does not use the Stream type on MongoOperations because Spring Data

Exception in monitor thread while connecting to server localhost:27017 while accessing MongoDB with Java

拈花ヽ惹草 提交于 2019-12-21 07:24:28
问题 I have the following exception when running Java app for MongoDB: [localhost:27017] org.mongodb.driver.cluster : Exception in monitor thread while connecting to server localhost:27017 while accessing MongoDB with Java Call stack is follows: com.mongodb.MongoSocketOpenException: Exception opening socket at com.mongodb.connection.SocketStream.open(SocketStream.java:63) ~[mongodb-driver-core-3.0.4.jar:na] at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:114)