spring-data-mongodb

How to write MongoTemplate Query and Criteria for a complex Document structure?

回眸只為那壹抹淺笑 提交于 2020-04-14 07:46:09
问题 I am new to MongoTemplate. I want to define a org.springframework.data.mongodb.core.query.Query and org.springframework.data.mongodb.core.query.Criteria to fetch the data from a collection which has a complex document structure. This is the document { "_id" : { "SId" : "ANBS", "AssetId" : "ANBS_BS21", "ST" : NumberLong(1479114000) //StartDate }, "ET" : NumberLong(1479117599) //EndDate, "TS" : [ NumberLong(1479114000), NumberLong(1479114600), NumberLong(1479115200), NumberLong(1479115800),

Spring Data Mongo: How to return nested object by its field?

感情迁移 提交于 2020-04-13 17:55:47
问题 I have domain: class Company { List<Job> jobs; } Is there a way to return nested object from collection like: @Repository public interface CompanyRepository extends MongoRepository<Company, String>{ Job findByJobId(String jobId); } 回答1: I have to make some assumptions about the structure of your Job model, but assuming something like this: public class Job { private String id; // other attributes and methods } ... and assuming that this model is embedded in your Company model, and not

Spring Data Mongo: How to return nested object by its field?

随声附和 提交于 2020-04-13 17:55:37
问题 I have domain: class Company { List<Job> jobs; } Is there a way to return nested object from collection like: @Repository public interface CompanyRepository extends MongoRepository<Company, String>{ Job findByJobId(String jobId); } 回答1: I have to make some assumptions about the structure of your Job model, but assuming something like this: public class Job { private String id; // other attributes and methods } ... and assuming that this model is embedded in your Company model, and not

Spring Data Mongo: How to return nested object by its field?

二次信任 提交于 2020-04-13 17:55:09
问题 I have domain: class Company { List<Job> jobs; } Is there a way to return nested object from collection like: @Repository public interface CompanyRepository extends MongoRepository<Company, String>{ Job findByJobId(String jobId); } 回答1: I have to make some assumptions about the structure of your Job model, but assuming something like this: public class Job { private String id; // other attributes and methods } ... and assuming that this model is embedded in your Company model, and not

Spring MongoDB Data elemMatch Simple

萝らか妹 提交于 2020-04-11 06:15:10
问题 { _id: 1, results: [ "tokyo", "japan" ] } { _id: 2, results: [ "sydney", "australia" ] } db.scores.find( { results: { $elemMatch: { $regex: *some regex* } } } ) How do you convert this simple elemMatch example using spring mongodb data Query Criteria? If the array contains object I can do it this way Criteria criteria = Criteria.where("results"). elemMatch( Criteria.where("field").is("tokyo") ); But in my question, I dont have the "field" Update: I thought the Veeram's answer was going to

Spring Data mongo case insensitive like query

自作多情 提交于 2020-04-08 09:23:25
问题 I want to make a text search case insensitive with regex query with spring-data mongo . For example in Oracle : select * from user where lower(username) like '%ab%' How can i make this query with spring-data mongo ? Thanks in advance 回答1: You can try something like below. Assumes you have a User pojo class. Using MongoTemplate i option for case insensitive: Criteria regex = Criteria.where("username").regex(".*ab.*", "i"); mongoOperations.find(new Query().addCriteria(regex), User.class); Using

java.lang.IllegalArgumentException: Expected unique result or null, but got more than one! - Spring Data Mongo

吃可爱长大的小学妹 提交于 2020-04-07 02:31:46
问题 I'm using Spring Boot v2.2.2.RELEASE and Spring Data MongoDB . In this example, I am looking perform group by department code and get all employees under that group. Sample Data - { "firstName" : "Laxmi", "lastName" : "Parate", "email" : "Laxmi.Parate@gmail.com", "createDate" : ISODate("2014-10-10T07:07:49.000Z"), ......... ......... "status" : "A", "departments" : { "deptCd" : "A", "deptName" : "Software Development", "status" : "A", } } It would be nice if we can do it using Spring Data

java.lang.IllegalArgumentException: Expected unique result or null, but got more than one! - Spring Data Mongo

妖精的绣舞 提交于 2020-04-07 02:28:34
问题 I'm using Spring Boot v2.2.2.RELEASE and Spring Data MongoDB . In this example, I am looking perform group by department code and get all employees under that group. Sample Data - { "firstName" : "Laxmi", "lastName" : "Parate", "email" : "Laxmi.Parate@gmail.com", "createDate" : ISODate("2014-10-10T07:07:49.000Z"), ......... ......... "status" : "A", "departments" : { "deptCd" : "A", "deptName" : "Software Development", "status" : "A", } } It would be nice if we can do it using Spring Data

java.lang.IllegalArgumentException: Expected unique result or null, but got more than one! - Spring Data Mongo

流过昼夜 提交于 2020-04-07 02:28:16
问题 I'm using Spring Boot v2.2.2.RELEASE and Spring Data MongoDB . In this example, I am looking perform group by department code and get all employees under that group. Sample Data - { "firstName" : "Laxmi", "lastName" : "Parate", "email" : "Laxmi.Parate@gmail.com", "createDate" : ISODate("2014-10-10T07:07:49.000Z"), ......... ......... "status" : "A", "departments" : { "deptCd" : "A", "deptName" : "Software Development", "status" : "A", } } It would be nice if we can do it using Spring Data

@Query named parameters for MongoDb Spring repository

…衆ロ難τιáo~ 提交于 2020-03-23 06:22:07
问题 Is it possible to use named parameters for a @Query method in a mongodb repository, just like we can do with a jpa repository (http://docs.spring.io/spring-data/jpa/docs/1.4.3.RELEASE/reference/html/jpa.repositories.html section 2.3.5)? As an example, I would like to use the following code: @Query("{'store' : :store, 'app' : :app }") List<T> findByStoreAndApp(@Param("store") String store, @Param("app") String app); instead of: @Query("{'store' : ?0, 'app' : ?1 }") List<T> findByStoreAndApp