spring-data-mongodb

Get sorted distinct values with MongoTemplate

风流意气都作罢 提交于 2020-06-27 19:36:09
问题 I am trying to get list of sorted distinct fields: public List<Object> getDistinctValues(String collection, String fieldName) { Query query = new Query(); query.with(new Sort(Sort.Direction.ASC, fieldName)); return mongoTemplate.findDistinct(query, fieldName, collection, Object.class); } but sorting isn't applied. Is there any way to do it with mongoTemplate? spring-boot-starter-data-mongodb: 2.1.2.RELEASE 回答1: Based on previous answer I solved my problem with Mongo Aggregation: @Override

Get sorted distinct values with MongoTemplate

跟風遠走 提交于 2020-06-27 19:35:30
问题 I am trying to get list of sorted distinct fields: public List<Object> getDistinctValues(String collection, String fieldName) { Query query = new Query(); query.with(new Sort(Sort.Direction.ASC, fieldName)); return mongoTemplate.findDistinct(query, fieldName, collection, Object.class); } but sorting isn't applied. Is there any way to do it with mongoTemplate? spring-boot-starter-data-mongodb: 2.1.2.RELEASE 回答1: Based on previous answer I solved my problem with Mongo Aggregation: @Override

Spring Data and MongoDB repository - how to create an update query?

旧城冷巷雨未停 提交于 2020-06-27 11:30:15
问题 I have the following jpa repository: @Query("UPDATE PlayerAccount pa SET pa.password = ?3 WHERE pa.id = ?1 AND pa.password = ?2") @Modifying public int updatePasswordWithValidation(Long playerAccountId, String oldPasswordDB, String encodePassword); Now, i would like to implement a similar update query for a mongoDB repository: @Query("update( { _id: ObjectId(' $1 ') }, { $set: { messageStatus: $2} })") But it doesn't work. any references to how a customized mongo repository update looks like?

How to create full text search query in mongodb with spring-data?

谁都会走 提交于 2020-06-26 06:14:22
问题 I have spring-data-mogodb application on java or kotlin, and need create text search request to mongodb by spring template. In mongo shell it look like that: db.stores.find( { $text: { $search: "java coffee shop" } }, { score: { $meta: "textScore" } } ).sort( { score: { $meta: "textScore" } } ) I already tried to do something but it is not exactly what i need: @override fun getSearchedFiles(searchQuery: String, pageNumber: Long, pageSize: Long, direction: Sort.Direction, sortColumn: String):

How to create full text search query in mongodb with spring-data?

谁都会走 提交于 2020-06-26 06:11:41
问题 I have spring-data-mogodb application on java or kotlin, and need create text search request to mongodb by spring template. In mongo shell it look like that: db.stores.find( { $text: { $search: "java coffee shop" } }, { score: { $meta: "textScore" } } ).sort( { score: { $meta: "textScore" } } ) I already tried to do something but it is not exactly what i need: @override fun getSearchedFiles(searchQuery: String, pageNumber: Long, pageSize: Long, direction: Sort.Direction, sortColumn: String):

Multi-Tenancy in Reactive Spring boot application using mongodb-reactive

主宰稳场 提交于 2020-06-12 07:46:09
问题 How can we create a multi-tenant application in spring webflux using Mongodb-reactive repository? I cannot find any complete resources on the web for reactive applications. all the resources available are for non-reactive applications. UPDATE: In a non-reactive application, we used to store contextual data in ThreadLocal but this cannot be done with reactive applications as there is thread switching. There is a way to store contextual info in reactor Context inside a WebFilter, But I don't

Java MongoDB save multiple documents at once

帅比萌擦擦* 提交于 2020-06-08 11:15:40
问题 I Have a list of updated objects/documents i need save all the objects in the list at once. I saw save() in MongoTemplate but it can take single document at a time. Is there any way to save multiple documents at once or i need to call save in loop ? 回答1: Thanks for all the help. I was able to do it using Spring data MongoDB. Spring data MongoDB's MongoRepository has many inbuilt methods. org.springframework.data.mongodb.repository.MongoRepository.saveAll(Iterable entites) is the one which i

Calling methods in two different ReactiveMongoRepository's in a transaction using Spring Data MongoDB?

情到浓时终转凉″ 提交于 2020-06-07 06:00:32
问题 When using the reactive programming model with Spring Data MongoDB it's possible to execute transactions like this: Mono<DeleteResult> result = template.inTransaction() .execute(action -> action.remove(query(where("id").is("step-1")), Step.class)); But Spring Data MongoDB also has support for "reactive repositories", for example: public interface PersonRepository extends ReactiveMongoRepository<Person, String> Flux<Person> findByLocationNear(Point location, Distance distance); } and public

Spring Data Mongo - apply unique combination fields in embedded document

白昼怎懂夜的黑 提交于 2020-06-07 04:42:45
问题 I'm working on Spring Boot v2.1.3.RELEASE & Spring Data Mongo . In this example, I want to apply uniqueness on email & deptName. The combination of email & deptName must be unique and is there any way to get email out since its repeating in each array object ? I tried below, but it's not working ! @CompoundIndexes({ @CompoundIndex(name = "email_deptName_idx", def = "{'email' : 1, 'technologyEmployeeRef.technologyCd' : 1}") }) Sample Data { "_id" : ObjectId("5ec507c72d8c2136245d35ce"), .... ..

Mongo query is not working for embedded documents

无人久伴 提交于 2020-05-28 09:35:29
问题 I'm using Spring Data Mongo and simple mongo query to get the list of Active Status departments. When I used below query, it still pulling the InActive records too. db.getCollection('employee').find({"departments.status" : "A"}) Sample data - { "firstName" : "Sichita", "lastName" : "Vinchurkar", "email" : "svinchurkar@gmail.com", "departments" : [ { "deptName" : "IT Support", ....... ....... "status" : "A" }, { "deptName" : "Mobile Development", ....... ....... "status" : "I" }, { "deptName"