spring-data-mongodb

Spring Data MongoDB - Criteria API OrOperator is not working properly

。_饼干妹妹 提交于 2019-12-03 05:43:33
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(criteriaV2)); List<Verb> verbList = mongoTemplate.find(query, Verb.class) But unfortunately verbList doesn't

Pagination with mongoTemplate

瘦欲@ 提交于 2019-12-03 05:08:42
I have a Query with Pageable: Query query = new Query().with(new PageRequests(page, size)) How can I execute it with MongoTemplate ? I don't see a single method returning Page<T> . MongoTemplate does not have methods to return Page . The find() methods return an ordinary List . with(new PageRequests(page, size) is used internally to adjust skip and limit with a MongoDB query (proceeded by a count query I think) Page can be used in conjunction with MongoDB repositories which is a specialized case of Spring data repositories. Thus, you'll have to use MongoRepository 's Page findAll(Pageable

MongoDB, Java: Retrieve date property as UTC

☆樱花仙子☆ 提交于 2019-12-02 18:15:46
问题 I'm saving some entities to a Mongo database, these have Joda DateTime properties which have UTC set as a timezone. While saving works fine and I see the properties with correct values in the collection, once I retrieve the entities through Java the timezone gets set to UTC+2 again. This is in the collection: "created" : ISODate("2013-07-26T20:36:57.890Z") I'm using Spring-Data-MongoDB to access the database. Category category = mongoTemplate.findById(id, Category.class); And I end up with

Project as nested document in spring mongo

拥有回忆 提交于 2019-12-02 13:52:09
问题 I'm looking for a translator to change this : getCollection('migrate').aggregate([ { "$project": { "Contrat": {"Field1":"$Field1", "Field2":"$Field2"}, "Formule": {"Field3":"$Field3", "Field4":"$Field4"} }}, { "$project": { "Contrats": {"Contrat":"$Contrat", "Formule":"$Formule"} }} ]) to MongoJava aggregation framework. Something like : AggregationOperation project = Aggregation.project("Field1,Field2"); // while naming it "Contrat" AggregationOperation project2 = Aggregation.project("Field3

Spring Data Mongo: upsert with various fields updated

偶尔善良 提交于 2019-12-02 11:32:01
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 be inserted in different moments: one during the first insertion, the second with a subsequent update. I

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

独自空忆成欢 提交于 2019-12-02 10:12:10
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 result in the format below in the mongo shell: { "result" : [ { "_id" : { "hour" : 5, "minute" : 51,

Spring mongo repository slice

强颜欢笑 提交于 2019-12-02 09:31:58
I am using spring-sata-mongodb 1.8.2 with MongoRepository and I am trying to use the mongo $slice option to limit a list size when query, but I can't find this option in the mongorepository. my classes look like this: public class InnerField{ public String a; public String b; public int n; } @Document(collection="Record") punlic class Record{ public ObjectId id; public List<InnerField> fields; public int numer; } As you can see I have one collection name "Record" and the document contains the InnerField. the InnerField list is growing all the time so i want to limit the number of the selected

Using conditional operator in a spring mongo [duplicate]

99封情书 提交于 2019-12-02 09:08:44
This question already has an answer here: How to use $cond operation in Spring-MongoDb aggregation framework 2 answers Following in my Mongo query {$project:{ id:"$_id", login:"$login", firstName: "$firstName", lastName:"$lastName", email:"$email", deactivateFlag:"$deactivateFlag", lastActivity:"$lastActivity", company :"$organization.name", RoleName :"$organization.roles.roleName", isMatchingRoles: { $eq: [ "$organization.roles.orgRoleId","$userOrgMap.roleId" ] } } }, { $match: {isMatchingRoles:true}, This works perfectly fine. Particularly the last $match works perfectly to reduce the

MongoRepository JSON Date Query (Spring)

梦想与她 提交于 2019-12-02 07:15:44
I am trying to use make my own query for a mongo Repository: @Repository public interface LogEntryRepository extends MongoRepository<LogEntry,String> { @Query("{'created_at' : {{ $gte: ISODate(?0)},{$lt: ISODate(?1)}}, " + "$or: [{'site': {$regex: ?2}}, {'login': {$regex: ?2}}, {'ip': {$regex: ?2}} ]" + "}") public Page<LogEntry> findByDateTimeBetweenAndCriteria(String isoStartDate, String isoEndDate, String searchTerm, Pageable page); } What I'd like to achieve is searching though dated logs with a keyword. The above complains about a parse error: Caused by: com.mongodb.util

@EnableMongoAuditing for MongoDB on Cloud Foundry / mongolab

≯℡__Kan透↙ 提交于 2019-12-02 06:48:49
问题 My setup works on my local but not when I deploy it to CloudFoundry/mongolab. The config is very similar to the docs. My local spring config @Configuration @Profile("dev") @EnableMongoAuditing @EnableMongoRepositories(basePackages = "com.foo.model") public class SpringMongoConfiguration extends AbstractMongoConfiguration { @Override protected String getDatabaseName() { return "myDb"; } @Override public Mongo mongo() throws Exception { return new MongoClient("localhost"); } @Bean public