spring-data-mongodb

Query interceptor for spring-data-mongodb for soft deletions

╄→гoц情女王★ 提交于 2019-12-10 16:53:39
问题 I want to add where condition to all repository fetching methods for not viewing deleted items. In Spring JPA it's possible to add @Where annotation to Entity. But for Spring Data MongoDB AFAIK it's not possible. Tried Mongodb lifecycle events but not succeeded. Is there a way of modifying repository queries before execution. 回答1: Can you explain what do you mean with "viewing deleted items"? If you want, you can use MongoTemplate and write your own repository and so can add desired where

Include Null check in MongoRepository query by example

落爺英雄遲暮 提交于 2019-12-10 16:29:42
问题 I Am trying to find all from a collection that match certain search criteria using MongoRepository using the following method: <S extends T> Page<S> findAll(Example<S> example, Pageable pageable); To do this I am building an ExampleMatcher for all fields that are included in the search. like so: private ExampleMatcher buildMatcher(FormSearchParameters formSearchParameters) { ExampleMatcher exampleMatcher = ExampleMatcher.matching() .withIgnorePaths("f1", "f2", "f3", "f4" , "f5"); if (

In spring data mongodb how to achieve pagination for aggregation

杀马特。学长 韩版系。学妹 提交于 2019-12-10 15:17:12
问题 In spring data mongodb using mongotemplate or mongorepository, how to achieve pagination for aggregateion 回答1: In addition to ssouris solution you can use Pageable classes for the results. public Page<UserListItemView> list(final Pageable pageable) { final Aggregation agg = newAggregation( skip(pageable.getPageNumber() * pageable.getPageSize()), limit(pageable.getPageSize()) ); final List<UserListItemView> results = mongoTemplate .aggregate(agg, User.class, UserListItemView.class)

Using Spring Data Mongodb, is it possible to get the max value of a field without pulling and iterating over an entire collection?

只谈情不闲聊 提交于 2019-12-10 13:57:34
问题 Using mongoTemplate.find() , I specify a Query with which I can call .limit() or .sort() : .limit() returns a Query object .sort() returns a Sort object Given this, I can say Query().limit(int).sort(), but this does not perform the desired operation, it merely sorts a limited result set. I cannot call Query().sort().limit(int) either since .sort() returns a Sort() So using Spring Data, how do I perform the following as shown in the mongoDB shell? Maybe there's a way to pass a raw query that I

how we would know if document was saved in Mongodb after save() method?

瘦欲@ 提交于 2019-12-10 13:37:04
问题 I am saving my document in MongoDb and using save method with class,collection name param. All the methods of save and insert are void return type. then how i can know that my document was saved or not. Is it that i have to re-query to check whether my document was saved. I just need any return value to see if it was saved. Moreover i am using Spring Data for Mongo to do operations. 回答1: It depends on what WriteConcern are you using. If you use WriteConcern.ACKNOWLEDGED , the operation will

Distinct() command used with skip() and limit()

左心房为你撑大大i 提交于 2019-12-10 13:19:17
问题 I have those items in my MongoDB collection: {x: 1, y: 60, z:100} {x: 1, y: 60, z:100} {x: 1, y: 60, z:100} {x: 2, y: 60, z:100} {x: 2, y: 60, z:100} {x: 3, y: 60, z:100} {x: 4, y: 60, z:100} {x: 4, y: 60, z:100} {x: 5, y: 60, z:100} {x: 6, y: 60, z:100} {x: 6, y: 60, z:100} {x: 6, y: 60, z:100} {x: 7, y: 60, z:100} {x: 7, y: 60, z:100} I want to query the distinct values of x (i.e. [1, 2, 3, 4, 5, 6, 7] ) ... but I only want a part of them (similar to what we can obtain with skip(a) and

How to save Timestamp type value in MongoDb | Java

泪湿孤枕 提交于 2019-12-10 12:58:07
问题 From Java driver, I want to save a document that looks like below json in MongoDb { "ts" : Timestamp(1421006159, 4)} Options I tried. Option 1: Map doc= new HashMap(1); doc.put("ts", new BSONTimeStamp()); It results in the below not required format {"ts" : { "_inc" : 0, "_class" : "org.bson.types.BSONTimestamp" }} Option 2: doc.put("ts",new Timestamp(new Date().getTime())); it results in : {"ts" : ISODate("2015-01-12T05:36:43.343Z")} 回答1: I used the following with the default mongodb-java

Spring Data MongoDB - Where to create an index programmatically for a Mongo collection?

喜欢而已 提交于 2019-12-10 12:47:09
问题 To create an index for a collection (as documented here https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/) one can use something like the following: mongoTemplate.indexOps(Person.class).ensureIndex(new Index().on("name",Order.ASCENDING)); But where in the program should I place this code snippet? In the relevant repository's constructor? I've did it like that now and it works, but I somehow feel like it is bad design. Somewhere in Mongo configuration? I haven't found a

Sorting number values stored as a BigDecimal type with Spring Data MongoDB in MongoDB

谁都会走 提交于 2019-12-10 10:56:02
问题 I'm studying Spring Data MongoDB with a tutorial (https://spring.io/guides/tutorials/data/2/). In this tutorial, the type of cost is BigDecimal as following and it is stored as a "string" in MongoDB. So, when I tried to get a sort result with an ascending sort by the cost field, I got a wrong result. I found that using the BigDecimal class is the best way for calculations. But, if I save numbers as a BigDecimal type with Spring Data MongoDB in MongoDB, it will be saved as a string type and I

How to set Mongodb Field Naming Strategy in Spring boot

时光总嘲笑我的痴心妄想 提交于 2019-12-10 10:43:39
问题 I am using spring boot 1.3.1.Release with Spring Data Mongodb and want to setup field naming strategy for fields. Below is my application.properties file: spring.data.mongodb.uri=mongodb://localhost/test spring.data.mongodb.field-naming-strategy=org.springframework.data.mongodb.core.mapping.SnakeCaseFieldNamingStrategy spring.data.mongodb.repositories.enabled=true spring.data.mongodb.database=test Everything is working fine except the field naming strategy. Any help will be highly appreciated