spring-mongo

How to add orderby using @query in mongodb repository

徘徊边缘 提交于 2019-12-05 04:21:19
I want to add orderby to the following repository method in mongodb with spring. I tried in various methods, but didnt work public interface StageRepository extends MongoRepository<Stage, String> { @Query("{$and: [ { 'categoryId': { $eq: ?0 } }, { 'isDeleted': { $eq: ?1 } } ]}") public List<Stage> findByCategoryIdAndIsNotDeleted(String categoryId, Boolean deleted); } I want to add orderby 'order' in the query. Not sure how to do it. Viresh You can do like : @Query("{$and: [ { 'categoryId': { $eq: ?0 } }, { 'isDeleted': { $eq: ?1 } } ]}") public List<Stage> findByCategoryIdAndIsNotDeleted

Spring Data mongo to insert null values to DB

风流意气都作罢 提交于 2019-12-05 00:33:27
问题 I am using Spring data mongo to insert a record to Mongo, here is my code mongoTemplate.save(person,"personCollection"); Here is my person object public class Person implements Serializable { int age; String address; String name; //Getters and setters including hashcode and equals } my address is null here , after inserting the record in the collection, the data is populated with only age and name i know that mongodb treats null value and noKey as the same thing, but my requirement is to even

Spring mongodb get ID of inserted item after Save

試著忘記壹切 提交于 2019-12-04 01:50:22
I am working with Spring MongoDb. I create various entities using insert method: http://docs.spring.io/spring-data/mongodb/docs/current/api/org/springframework/data/mongodb/core/MongoOperations.html#insert-java.lang.Object- However, all methods return void . I need to return the ObjectId of the inserted document. What is the best way to get it? Simon This is pretty interesting and thought I would share. I just figured out the solution for this with the help of BatScream comment above: You would create an object and insert it into your MongoDB: Animal animal = new Animal(); animal.setName(name)

Spring Data mongo to insert null values to DB

百般思念 提交于 2019-12-03 15:57:56
I am using Spring data mongo to insert a record to Mongo, here is my code mongoTemplate.save(person,"personCollection"); Here is my person object public class Person implements Serializable { int age; String address; String name; //Getters and setters including hashcode and equals } my address is null here , after inserting the record in the collection, the data is populated with only age and name i know that mongodb treats null value and noKey as the same thing, but my requirement is to even populate the address:null to have the schema consistent how do i do this with Spring Data mongo

Filter array in sub-document array field in Spring framework

我们两清 提交于 2019-12-02 14:36:13
问题 I am trying to fetch an element from an array in the MongoDB in my Spring Framework project. I have find the solution for MongoDB shell, but I do not know how to implement it by Spring.data.core.aggregation, one of aggregation operator @addFields is not supported by Spring. Could anyone tell me how to replace this @addField or how to implement in it another way? Thank you so much!!! MongoDB sample data: { "_id" : 15, "items" : [ { "columns" : [ { "title" : "hhh", "value" : 10 }, { "title" :

Filter array in sub-document array field in Spring framework

两盒软妹~` 提交于 2019-12-02 10:52:53
I am trying to fetch an element from an array in the MongoDB in my Spring Framework project. I have find the solution for MongoDB shell, but I do not know how to implement it by Spring.data.core.aggregation, one of aggregation operator @addFields is not supported by Spring. Could anyone tell me how to replace this @addField or how to implement in it another way? Thank you so much!!! MongoDB sample data: { "_id" : 15, "items" : [ { "columns" : [ { "title" : "hhh", "value" : 10 }, { "title" : "hahaha", "value" : 20 } ] }, { "columns" : [ { "title" : "hiii", "value" : 50 } ] } ] } Expected result

Mongo Connection Pooling(Changing the size of connection pool)

六月ゝ 毕业季﹏ 提交于 2019-12-02 04:50:49
问题 How to change the mongo connection pool size? I have seen it is 100 by default. Is there a way to change this value? I dont want to do it via spring, is there a way to configure it via MongoClient? There is an option i see about mongoClientOptions but i dont see options to set connection pool 回答1: You can build your own MongoClient instance using the MongoClientOptions.Builder. MongoClientOptions.Builder builder = new MongoClientOptions.Builder(); MongoClientOptions options = builder

Mongo Connection Pooling(Changing the size of connection pool)

我只是一个虾纸丫 提交于 2019-12-02 02:39:08
How to change the mongo connection pool size? I have seen it is 100 by default. Is there a way to change this value? I dont want to do it via spring, is there a way to configure it via MongoClient? There is an option i see about mongoClientOptions but i dont see options to set connection pool You can build your own MongoClient instance using the MongoClientOptions.Builder . MongoClientOptions.Builder builder = new MongoClientOptions.Builder(); MongoClientOptions options = builder.connectionsPerHost(10).build(); MongoClient client = new MongoClient(listOfServers, options); As another option(and

What is the correct way to close the mongo connection using spring-mongo?

最后都变了- 提交于 2019-12-01 10:50:55
问题 I am using spring-mongo in my webapp. When I undeploy my application in Tomcat7, there is a memory leak. I suspect that it might be the Mongo object that I didn't explicitly close. I would like to know what is the correct way (and location) to close it. 回答1: How about something like this: @Component public class MongoDBManager { @Autowired Mongo mongo; @PreDestroy public void shutdown() { mongo.close(); } } 来源: https://stackoverflow.com/questions/13721115/what-is-the-correct-way-to-close-the

MongoDB comparison operators with null

别说谁变了你拦得住时间么 提交于 2019-12-01 03:14:30
In MongoDB I would like to use $gt and $lt comparision operators where the value could be null . When the operators did not work with null , I looked for documentation but found none. In both cases it returned no documents (even though $ne, $gte, and $lte did return documents; meaning there were documents that were both equal to and not equal to null ). I would expect $gt to essentially operate like $ne (as the null type Mongo comarison order is so low) and $lt to return nothing for the same reason. I was hoping this would work as the value I pass to the query is variable (potentially null ),