spring-data-mongodb

Mongo query is not working for embedded documents

a 夏天 提交于 2020-05-28 09:35:24
问题 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"

Mongo query is not working for embedded documents

﹥>﹥吖頭↗ 提交于 2020-05-28 09:35:09
问题 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"

How to get a binary stream by GridFS ObjectId with Spring Data MongoDB

大憨熊 提交于 2020-05-24 21:23:29
问题 I can't figure out how to stream a binary file from GridFS with spring-data-mongodb and its GridFSTemplate when I already have the right ObjectId . GridFSTemplate returns either GridFSResource ( getResource() ) or GridFSFile ( findX() ). I can get the GridFSFile by ID: // no way to get the InputStream? GridFSFile file = gridFsTemplate.findOne(Query.query(Criteria.where("_id").is(id))) but there is no obvious way how to get an InputStream for that GridFSFile . Only GridFSResource allows me to

Spring Data MongoDB - Aggregation with other collection as well

ε祈祈猫儿з 提交于 2020-05-23 11:54:51
问题 I'm working on Spring Boot v.2.1.3.RELEASE and Spring Data MongoDB . Due to critical requirement, I had to model like below assuming Employee knows multiple technologies, but primary language would be anyone. So, I decided to keep technology collection separate and somehow related Employee and technology in the Employee Collection. { "_id" : ObjectId("5ec65750fdcd4e960f4b2f24"), "technologyCd" : "AB", "technologyName" : "My ABC", "ltechnologyNativeName" : "XY", "status" : "A" } So, I've done

Do db.collection.replaceOne by spring data mongo

戏子无情 提交于 2020-05-17 14:42:47
问题 I want to replace whole document in mongo collection in one update instead of "query and update". It seems the db.collection.replaceOne can do this job, but I can't find any api in the MongoOperations. 回答1: Just call save() on MongoTemplate which will invoke replaceOne with UpdateOptions().upsert(true) . 来源: https://stackoverflow.com/questions/49749209/do-db-collection-replaceone-by-spring-data-mongo

Group by id doesn't work in spring data mongodb

百般思念 提交于 2020-05-17 06:08:06
问题 Im trying to use Aggregation in Spring data with mongodb. After few stages unwind , lookup , match I come up with the sample following data which is after the projection, then I try to group it by _id . { "_id": 1, "name":"Maths" }, { "_id": 1, "name":"Maths" }, { "_id": 2, "name":"Science" }, { "_id": 2, "name":"Science" } The following mongo script is working perfectly. { $project: { name: 1 } }, { $group: { _id: '$_id', name: { $first: '$name' } } } When I do it in spring, group("_id")

Spring Data Mongo update non-null fields of object?

落爺英雄遲暮 提交于 2020-05-17 05:12:31
问题 If I have a Java object with only updated fields set, for example (assume there is a third field C that is not set): obj.setA(1); obj.setB(2); Is it possible to perform an Update operation that only updates A and B ? It appears my only options with Spring Data are to use save() (which would overwrite the value for C in the database to null ), or use update() , which requires me to construct an Update object with a set() statement for each field in the object, as well as hardcode Mongo field

Spring Data Mongo - Perform Distinct, but doesn't wants to pull embedded documents in results

江枫思渺然 提交于 2020-05-08 05:56:05
问题 I'm developing Spring Boot and Spring Data Mongo example. In this example, I want to get the distinct departments only, but I dont want to fecth subdepartments. What Query do I need to change? db.employees.distinct("departments"); Data: { "firstName" : "Laxmi", "lastName" : "Dekate", ..... ....... ..... "departments" : { "deptCd" : "Tax", "deptName" : "Tax Handling Dept", "status" : "A", "subdepts" : [ { "subdeptCd" : "1D", "subdeptName" : "Tax Clearning", "desc" : "", "status" : "A" } ] }, }

Spring Mongodb - Cannot write custom Converter for java.time.Period

六月ゝ 毕业季﹏ 提交于 2020-04-20 15:59:53
问题 I'm using Spring Cloud Brixton.SR4 with Spring Data MongoDB. I have a very simple entity: @Document public class Foo{ private Period period; //getter & setter } Because java.time.Period is not supported by jsr310 I'm creating custom converters: class Converters { @Component @WritingConverter static class PeriodToStringConverter implements Converter<Period, String> { @Override public String convert(Period period) { return period.toString(); } } @ReadingConverter @Component static class

Spring Boot WebFlux Reactive MongoDB: how to switch the database on each request?

会有一股神秘感。 提交于 2020-04-18 05:35:15
问题 I'm working on a SaaS project using Spring WebFlux and Reactive MongoDB. It needs to be a MultiTenant application and each tenant must use its own database. As for now I just added the Reactive MongoDB dependency to the pom.xml : <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId> <version>2.2.6.RELEASE</version> </dependency> Then I extended the AbstractReactiveMongoConfiguration in order to provide the MongoClient and