spring-data-mongodb

Spring Data mongodb: adding credentials for MongoDb access

拥有回忆 提交于 2019-12-04 13:11:29
I have the following working configurations in my Spring application: <mongo:mongo id="myRs" replica-set="myhost:27017"> <mongo:options max-wait-time="1500" connect-timeout="30000" /> </mongo:mongo> <bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"> <property name="writeResultChecking" value="EXCEPTION"/> <property name="writeConcern" value="FSYNC_SAFE"/> <constructor-arg ref="myRs"/> <constructor-arg name="databaseName" value="mydb"/> </bean> Now all I want to do is to set up username/password for accessing the mongo database without changing my code (i.e.

How to properly load and configure Spring beans from an external utility jar

牧云@^-^@ 提交于 2019-12-04 12:47:54
Currently I have a utility jar that contains a number of datastore services. Behind the scenes these datastore services use Spring Data MongoDB, and everything is configured using an app-context.xml file in the utility jar. I want this utility jar to be able to change the backing store without having to change anything that uses this utility jar. Now, I want to create a spring mvc web application that uses the datastore services from this utility jar. How do I set this up so that the spring mvc web app (or any other jar) can easily use the datastore services without having to know too much

Spring data MongoDB adding arrays to an existing document

安稳与你 提交于 2019-12-04 12:15:28
Say I have the following Collections public @Data class Customer { @Id private String id; private String firstName; private String lastName; @DBRef private List<Address> addressList= new ArrayList<Address>(); } and public @Data class Address { @Id private String id; private String address; private String type; private String customerID; } And each Customer has multiple addresses, and I have implemented MongoRepository . Saving customer for the First time is working pretty well customerRepo.save(customerObject) and before calling the save I am persisting multiple Address Objects and then

Build dynamic queries with Spring Data MongoDB Criteria

笑着哭i 提交于 2019-12-04 10:43:14
I would like to run a bulk delete operation on a list of documents in MongoDB that have been selected by the user in the UI so I need to dynamically build a query that looks like the following (the or clause expands for every document selected): { $and: [ { "contentType": "application/vnd.sometype" }, { $or: [ { "metadata.name": "someName", "metadata.version": "someVersion" }, { "metadata.name": "someOtherName", "metadata.version": "someOtherVersion" } ] } ] }, Fields: null, Sort: null Just now I'm using string concatenation to achieve this. Is it possible to build this query with the Spring

Spring Data MongoDB - Criteria API OrOperator is not working properly

随声附和 提交于 2019-12-04 10:22:19
问题 I'm facing Spring Data MongoDB Criteria API orOperator problem. Here's query result for irregular verbs: ( Terminal output ) > db.verb.find({'v2':'wrote'}); { "_id" : ObjectId("5161a8adba8c6390849da453"), "v1" : "write", "v2" : "wrote", "v3" : "written" } And I query verbs by their v1 or v2 values using Spring Data MongoDB Criteria API : Criteria criteriaV1 = Criteria.where("v1").is(verb); Criteria criteriaV2 = Criteria.where("v2").is(verb); Query query = new Query(criteriaV1.orOperator

performance issue on Spring Data Mongodb

最后都变了- 提交于 2019-12-04 08:05:54
I've got an issue on spring data mongodb, in a method, I request a simple "find" wich retrieve ~1000 Documents. my spring data code is here : Query myquery = query(where("ipp").is(ipp).and(CODE_MESURE).in(codes).and(DATE_MESURE).gte(iDateDebut).lt(iDateFin)); return template.find(myquery, MessageMongo.class); And with JProfiler, i've got ~1,4sec in the "find" method of MongoTemplate class. Note : The request to MongoDB is not the problem, execution take less than 20ms. But if try to request the same query with mongo java driver by traditional way : final DBCollection collection = template

Prevent Spring Data for Mongo to convert ids to ObjectId

这一生的挚爱 提交于 2019-12-04 07:28:46
I'm using Spring Data for Mongo on an existing database. The previous application used plain strings for ids instead of ObjectId. My problem is that Spring Data insists on converting the strings to ObjectId, which makes all queries by id to fail. For example, when I do repository.findOne('') , the query executed is { "_id" : { "$oid" : "50cf9f34458cf91108ceb2b4"}} when it should be { "_id" : "50cf9f34458cf91108ceb2b4" } Is there a way to avoid Spring Data to convert string ids to ObjectId? Thanks! Diego I finally found a solution for this. Probably not the best option, but works. What I did

MongoDB: how to group by date with mongo date aggregration using java driver

本小妞迷上赌 提交于 2019-12-04 06:32:15
问题 I have been struggling writing to convert a mongo shell command to java query on top of the java mongo driver in order to call it in my webapp project: the command is as follow: db.post.aggregate( { $match: { dateCreated: { "$gt": new ISODate("2013-08-09T05:51:15.000Z"), "$lt": new ISODate("2013-08-09T05:51:20.000Z") } } }, { $group: { _id: { hour: {$hour: "$dateCreated"}, minute: {$minute: "$dateCreated"}, second: {$second: "$dateCreated"} }, cnt: {$sum : 1} } } ) The query above outputs

Mongo DB request in Java Spring-data Mongo

十年热恋 提交于 2019-12-04 05:59:40
问题 I have an array inside of document. { "id" : "id_1", "name" : "name_1"; "additionalData" : [ { "additionalDataId" : "id_1_1", "additionalDataName" : "name_1_1", "longText" : "A long story about..." }, { "additionalDataId" : "id_1_2", "additionalDataName" : "name_1_2", "longText" : "A longer story about danger..." }, { "additionalDataId" : "id_1_3", "additionalDataName" : "name_1_3", "longText" : "A longer story about danger and courage" }, ] } To retrieve element of array with name "name_1_2"

Spring Data Mongo: upsert with various fields updated

爱⌒轻易说出口 提交于 2019-12-04 05:39:28
问题 I am searching for the right way to implement the upsert operation to a Mongo Collection using the API given by Spring Data. In details, I have the following use case. The schema of the collection collection is something like the following: { _id: "some_id", field1: "value1", field2: "value2", subdocument1: { // A subdocument with some fields }, subdocument2: { // A subdocument with some other fields } } The fields field1 and field2 are always present, but subdocument1 and subdocument2 will