spring-data-mongodb

MongoRepository JSON Date Query (Spring)

99封情书 提交于 2019-12-04 05:17:22
问题 I am trying to use make my own query for a mongo Repository: @Repository public interface LogEntryRepository extends MongoRepository<LogEntry,String> { @Query("{'created_at' : {{ $gte: ISODate(?0)},{$lt: ISODate(?1)}}, " + "$or: [{'site': {$regex: ?2}}, {'login': {$regex: ?2}}, {'ip': {$regex: ?2}} ]" + "}") public Page<LogEntry> findByDateTimeBetweenAndCriteria(String isoStartDate, String isoEndDate, String searchTerm, Pageable page); } What I'd like to achieve is searching though dated logs

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

人盡茶涼 提交于 2019-12-04 05:12:52
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 save multiple times the version object over time, I have a duplicate keys exception. I think it's the

mongotemplate aggregation with condition

房东的猫 提交于 2019-12-04 03:47:54
问题 I have a collection where the documents look like this: { _id: "545b9fa0dd5318a4285f7ce7", owner: "admin", messages: [ { id: "100", status: "sent", note: "" }, { id: "100", status: "pending", note: "" }, { id: "101", status: "sent", note: "" }, { id: "102", status: "sent", note: "" }, { id: "101", status: "done", note: "" } ] } (This is just a short example, in my case the sub array is very large) I need to query the collection and get some statistics for a specific document. So in this

Persisting Objects containing Objects with spring-data-mongodb

雨燕双飞 提交于 2019-12-04 03:28:35
问题 Below is a follow-up question to Question 13832188: I'm using spring-data-mongodb version 1.1.1.RELEASE . I am able to persist an object if all the member variables are primitive types, even if the names of the @PersistenceConstructor arguments don't match exactly the names of the member variables by using the @Field and @Value annotations. However, I get a MappingInstantiationException when I try to persist objects that contain other objects. My questions: Is this a bug in spring-data

Spring data mongoDb not null annotation like Spring data Jpa

本小妞迷上赌 提交于 2019-12-04 02:27:48
Like spring-data-jpa have @NotNull annotation what can be used for this in spring-data-mongodb.? javax.validation.constraints.NotNull itself could be used with spring-data-mongodb. For this you need to have following in place. JSR-303 dependencies added in your pom.xml <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>5.3.4.Final</version> </dependency> Declare appropriate validators and validator event listeners import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org

How to use optimistic locking with Spring Data MongoDB?

浪尽此生 提交于 2019-12-04 00:54:11
问题 I'm going through Spring Data MongoDB - Reference Documentation and I'm finding the examples to be a bit too simplistic. In particular I'm trying to understand how to handle stale data in concurrent environments. For instance, imagine I have the following entity: public class Person { private final String username; private final String firstname; private final String lastname; [...] } Now if I use CrudRepository to save/update/remove my entities, then imagine a scenario in which two threads

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

半腔热情 提交于 2019-12-04 00:44:39
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) ~[mongodb-driver-core-3.0.4.jar:na] at com.mongodb.connection.DefaultServerMonitor

Text Search not working with Spring Boot MongoDB

谁说胖子不能爱 提交于 2019-12-03 23:03:28
I am developing the Spring Boot + MongoDB + spring data mongo + Text search Example. By taking a reference from link: https://spring.io/blog/2014/07/17/text-search-your-documents-with-spring-data-mongodb , I developed my code, but when I am executing that, it's giving empty resultset. Please help in this scenario. I was expecting to get both results OrderId = 10248 and 10249 , but I got empty. My developed code: OrderDetails.java @Document(collection="order-details") public class OrderDetails { @Id private ObjectId id; //@TextIndexed(weight=2) @Field("OrderID") private Integer orderID; @Field(

how to pivot data using spring-data-mongodb

十年热恋 提交于 2019-12-03 21:36:41
I have data of this form and I referred to this link pivoting in mongodb : Name, SumPrice, type,year A, 50.0, Retail,2015 B, 467.0, Online,2015 A, 1456.0, Direct,2015 B, 1149.53, Direct,2015 C, 273.20, Online,2014 C, 110.0, Direct,2014 I want data to be transformed using spring-data-mongodb of this form Name ,Retail, Online , Direct, year A, 50.0 , 0.0, 1456.0 2015 B, 0.0, 467.0, 1149.53, 2015 C, 0.0, 273.20, 110.0, 2014 Please lemme know how to do. Regards kris 来源: https://stackoverflow.com/questions/32902502/how-to-pivot-data-using-spring-data-mongodb

Mongotemplate.updateFirst() doesn't update @LastModifiedDate

风格不统一 提交于 2019-12-03 20:53:32
I'm using annotation based configuration. @Configuration @Profile("default") @ComponentScan(basePackages = "com.*") @EnableMongoRepositories @EnableMongoAuditing public class ApplicationDataConfig .... And I also have @LastModifiedDate , @CreatedDate annotation set on fields of type org.joda.time.DateTime . When I use org.springframework.data.mongodb.repository.MongoRepository.save(entity) both the audit fields are updated just fine. But when I use org.springframework.data.mongodb.core.MongoTemplate.updateFirst()/updateMulti() the lastupdated time is not being updated. Does anybody have a clue