spring-data-mongodb

spring data mongodb _id mapping preference

强颜欢笑 提交于 2019-11-29 18:00:00
问题 I am using spring data mongodb framework in my java application to persist my application data in mongodb. In my java model class I have two fields. 1) A field objId with @Id annotation on it. 2) A field id with respect to mapping to _id key in the mongodb document saved, which one will get preference or will I get an error for multiple mapping. I am using spring data mongo 1.6.1. I know I could have tested this out but I do not have local environment setup. 回答1: The answer is in

MongoDB $pull array 2 level

橙三吉。 提交于 2019-11-29 16:29:34
I'm trying to pull an element in an array with e two level deep complexity My document : > db.users.find({ mail : 'titi@toto.fr'}).pretty() { "_class" : "bean.User", "_id" : ObjectId("52f504bb2f9dd91186211537"), "commandes" : [ { "adresse" : "15 rue de la soif", "codePostal" : "29200", "idCommande" : 1, "montantTotal" : 0, "nom" : "TOTO", "prenom" : "tata", "ville" : "Brest", "voyagesSouscrits" : [ { "idVoyage" : "123", "duree" : 1, "nbPersonnes" : 0, "villeDepart" : "Nantes", "prixVoyage" : 999999 }, { "idVoyage" : "addVoyageSouscrit", "duree" : 1, "nbPersonnes" : 0, "villeDepart" : "Toulouse

Spring Data MongoDB Annotation @CreatedDate isn't working, when ID is assigned manually

我的未来我决定 提交于 2019-11-29 14:28:33
问题 I'm trying to use auditing to save dateCreated and dateUpdated in my objects, but since I set ID manually, there's some additional work. Following Oliver Gierke's suggestion in DATAMONGO-946 I'm trying to figure out how to correctly implement it. As original poster in Jira task above, I've downloaded example from here https://github.com/spring-guides/gs-accessing-data-mongodb.git and modified it a bit: package hello; import org.springframework.data.annotation.CreatedDate; import org

Mongo unique index case insensitive

*爱你&永不变心* 提交于 2019-11-29 11:37:31
@CompoundIndexes({ @CompoundIndex(name = "fertilizer_idx", unique = true, def = "{'name': 1, 'formula': 1, 'type': 1}") }) public class Fertilizer extends Element implements Serializable { //class stuff } Is it possible to create the index case insensitive? Right now it is differentiating from NAME to NAMe . Saving a second field lowercase (or uppercase) is not a possibility for me. Thanks, Pedro Prior of MongoDB version 3.4 we were unable to create index with case insensitive . In version 3.4 has collation option that allows users to specify language-specific rules for string comparison ,

Spring Data MongoDB Repository - JPA Specifications like

时光总嘲笑我的痴心妄想 提交于 2019-11-29 11:30:57
Is there something like JPA Specifications for Spring Data MongoDB Repositories? If not, how can I make dynamic queries with repositories? A classic scenario could be a search form with optional fields that the user will fill. I found myself a way. The trick can be done using QueryDSL , in the following way: First, add the QueryDSL dependencies: <dependency> <groupId>com.mysema.querydsl</groupId> <artifactId>querydsl-mongodb</artifactId> <version>${querydsl-mongo.version}</version> </dependency> <dependency> <groupId>com.mysema.querydsl</groupId> <artifactId>querydsl-apt</artifactId> <version>

How to use spring data mongo @CompoundIndex with sub collections?

巧了我就是萌 提交于 2019-11-29 09:37:42
问题 Assume that I have such entities like the following: @Document(collection = "doc_a") public class A { @Field("id") private Integer id; @Field("b") private Collection<B> b; ... } public class B { @Field("id") private Integer id; ... } is it possible to use a compoundIndex with respect to A.id AND B.id together? I mean maybe like: @CompoundIndex(name = "aid_bid_idx", def = "{'id', 'b.id'}") Thanks in advance. 回答1: I've tried this kind of compound index in my app, that use spring data too, and

Mapping a document with partly-defined schema

纵然是瞬间 提交于 2019-11-29 09:00:57
I'm writing a demo app using Spring & MongoDB as a database. My main domain class looks like: @Document public class Person { @Id private String id; //Some other fields private DBObject additionalData; } The key is that additionalData is a subdocument with no schema specified, it is kind of user-defined JSON. But when I am parsing this json (using (DBObject) JSON.parse(value) expression), it is stored as a string in MongoDB, and I need it to be a nested document structure. Searched for couple of hours, found no solution. Any ideas? Marc Tarin I'm not really sure of the expected result of

Optional cannot be returned by stream() in Mockito Test classes

点点圈 提交于 2019-11-29 08:59:52
I am developing Test cases for the Spring Data Mongo Services. org.mockito.exceptions.misusing.WrongTypeOfReturnValue: Optional cannot be returned by stream() stream() should return Stream *** If you're unsure why you're getting above error read on. Due to the nature of the syntax above problem might occur because: 1. This exception *might* occur in wrongly written multi-threaded tests. Please refer to Mockito FAQ on limitations of concurrency testing. 2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies - - with doReturn|Throw() family of methods. More in

How to Get Only Matched Result from An Array Field of Document in Mongo Using Spring Data

ε祈祈猫儿з 提交于 2019-11-29 07:47:54
I am using spring boot 1.5.1 and MongoDB version 3.4.6 . I have a mongo document Hotel which has a list of Review . The Review class has property userName . @Document public class Hotel { @Id private String id; private List<Review> reviews; I want to search all the hotel by Review userName. My HotelRepository has public List<Hotel> findByReviewsUserName(String userName); When I am calling with user 'Salman' - List<Hotel> list = this.hotelRepository.findByReviewsUserName(user); this method returns result like below : [ { "id": "59b23c39c70ff63135f76b14", "name": "Signature", "reviews": [ { "id"

Spring data mongodb not closing mongodb connections

若如初见. 提交于 2019-11-29 07:26:28
I am using spring-data-mongodb (1.7.0.RELEASE) with spring-webmvc framework for my web application. I am using basic CRUD functions using mongoRepository but i am not closing mongo connections in my code cause i thought that spring-data-mongodb will close it by itself, But it keeps on opening new connections and not closing them. These too many connections ares crashing my application and i have to restart tomcat again and again (twice a day) to overcome this. Note: Spring Application & mongod is on same server. This is log just after crashing - 2015-07-17T01:31:20.068-0400 I NETWORK [conn3645