spring-data-mongodb

Spring data MongoDb: MappingMongoConverter remove _class

戏子无情 提交于 2019-11-28 05:19:09
The default MappingMongoConverter adds a custom type key ("_class") to each object in the database. So, if I create a Person: package my.dto; public class Person { String name; public Person(String name) { this.name = name; } } and save it to the db: MongoOperations ops = new MongoTemplate(new Mongo(), "users"); ops.insert(new Person("Joe")); the resulting object in the mongo will be: { "_id" : ObjectId("4e2ca049744e664eba9d1e11"), "_class" : "my.dto.Person", "name" : "Joe" } Questions: What are the implications of moving the Person class into a different namespace? Is it possible not to

Mongo unique index case insensitive

自闭症网瘾萝莉.ら 提交于 2019-11-28 04:57:19
问题 @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 回答1: Prior of MongoDB version 3.4 we were unable to create index with case insensitive . In

updating object with spring data mongodb and kotlin is not working

∥☆過路亽.° 提交于 2019-11-28 04:37:52
问题 I have the following request handler fun x(req: ServerRequest) = req.toMono() .flatMap { ... val oldest = myRepository.findOldest(...) // this is the object I want to modify ... val v= anotherMongoReactiveRepository.save(Y(...)) // this saves successfully myRepository.save(oldest.copy( remaining = (oldest.remaining - 1) )) // this is not saved ok().body(...) } and the following mongodb reactive repository @Repository interface MyRepository : ReactiveMongoRepository<X, String>, ... { } The

Spring-data-mongodb connect to multiple databases in one Mongo instance

扶醉桌前 提交于 2019-11-28 04:33:20
I am using the latest spring-data-mongodb (1.1.0.M2) and the latest Mongo Driver (2.9.0-RC1). I have a situation where I have multiple clients connecting to my application and I want to give each one their own "schema/database" in the same Mongo server. This is not a very difficult task to achieve if I was using the driver directly: Mongo mongo = new Mongo( new DBAddress( "localhost", 127017 ) ); DB client1DB = mongo.getDB( "client1" ); DBCollection client1TTestCollection = client1DB.getCollection( "test" ); long client1TestCollectionCount = client1TTestCollection.count(); DB client2DB = mongo

Mapping a document with partly-defined schema

邮差的信 提交于 2019-11-28 02:19:23
问题 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

Spring data Match and Filter Nested Array

别来无恙 提交于 2019-11-28 02:04:45
How can extract data from nested array ? I want to extract the array item "values" where wind_speed parameter value is between vitRange.min and vitRange.max (same condition for twaRange and wind direction) Data : { "name" : "race" ,"polaire" : [ { "voile" : "foc" , "matrice" :[ { "vitRange" : { "min" : 0, "max" : 4} ,"twaRange" : { "min" : 0, "max" : 30} ,"values" : [0, 0, 0, 2.4] }, { "vitRange" : { "min" : 4, "max" : 6} ,"twaRange" : { "min" : 30, "max" : 33} ,"values" : [0, 0, 2.4, 3.7] } ] }, { "voile" : "spi" , "matrice" :[ { "vitRange" : { "min" : 0, "max" : 4} ,"twaRange" : { "min" : 0,

Spring data mongodb not closing mongodb connections

别来无恙 提交于 2019-11-28 01:07:06
问题 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 &

How to run js file in mongo using spring data

谁说胖子不能爱 提交于 2019-11-28 01:00:36
问题 In mongo shell js file can be run using load command: load("path/to/file/file.js") How to do this using spring-data? Or any other way in Java. I've tried: BasicDBObject obj = new BasicDBObject(); obj.append( "$load" , "/path/file.js" ); CommandResult t=mongoTemplate.executeCommand(obj); and: obj.append( "$eval" , "load(\"/path/file.js\")" ); but it doesn't work. 回答1: Here's the relevant section of the reference docs on how to work with scripts in Spring Data MongoDB. ScriptOperations

Mongodb aggregation query to subtract and grouping of cumulative value

僤鯓⒐⒋嵵緔 提交于 2019-11-28 00:52:10
问题 { "_id" : ObjectId("58f5a22d22679039176d2ee8"), "MachineID" : NumberInt("1001"), "Timestamp" : ISODate("2017-04-18T07:01:01.000+05:30"), "Utilization" : NumberInt("63654480"), "RunStatus" : NumberInt("1"), "ProductsCount" : NumberInt("681350") }, { "_id" : ObjectId("58f5a22d22679039176d2ee9"), "MachineID" : NumberInt("1001"), "Timestamp" : ISODate("2017-04-18T07:02:02.000+05:30"), "Utilization" : NumberInt("63655480"), "RunStatus" : NumberInt("1"), "ProductsCount" : NumberInt("681370") }, { "

Spring data mongodb search for ISO date

人盡茶涼 提交于 2019-11-27 21:38:33
I am trying to search for date of birth using query criteria = Criteria.where("dob").lte(new DateTime().toDate()); And spring data mongodb generate below query: MongoTemplate: find using query: { "dob" : { "$lte" : { "$date" : "2015-05-16T07:55:23.257Z"}}} fields: null for class: class com.temp.model.User in collection: user But I did not get any result. My dob field in mongodb: {"dob" : ISODate("1991-01-23T00:00:00Z")} How I can search for dob in ISODate format ? moonlighter This code should work fine for what you need: criteria = Criteria.where("dob").lte(new java.util.Date()); My test is