1

Can I have difference between two dates to greater than 0 in Criteria operator in spring-data-mongodb? I wrote the query below :

  Criteria c= Criteria.where("myDate").gte(startDate).
               andOperator(Criteria.where("myDate").lte(endDate).andOperator(Criteria.where("studentId").is(studentId).andOperator(Criteria.where("currDate - myDate").gt(0))));

This query not working. If possible please help me in getting this query work with spring-data-mongodb.

Edit: The mongodb pipeline query is as follows:

 { "aggregate" : "__collection__" , "pipeline" : [ { "$match" : { "myDate" : { "$gte" : { "$date" : "2000-01-01T07:57:33.231Z"}} , "$and" : [ { "myDate" : { "$lte" : { "$date" : "2015-11-05T07:57:33.231Z"}} , "$and" : [ { "studentId" : "100" , "$and" : [ { "currDate - myDate" : { "$gt" : 0}}]}]}]}} , { "$project" : { "status" : 1}} , { "$group" : { "_id" : { "status" : "$status"} , "activeCount" : { "$sum" : 1}}}]}

Regards

Kris

chiku
  • 485
  • 2
  • 8
  • 23

1 Answers1

1

For it to work, you'd essentially want to convert the current aggregation pipeline to this:

var pipeline = [ 
    { 
        "$project" : { 
            "status" : 1,
            "studentId" : 1,
            "myDate" : 1,
            "dateDifference": { "$subtract": [ new Date(), "$myDate" ] }
        }
    },
    { 
        "$match" : { 
            "studentId": "100" ,
            "myDate": { 
                "$gte": ISODate("2000-01-01T07:57:33.231Z"),
                "$lte": ISODate("2015-11-05T07:57:33.231Z")
            },
            "dateDifference": { "$gt" : 0 }         
        }
    },   
    { 
        "$group": { 
            "_id": "$status",           
            "activeCount": { "$sum" : 1 }
        }
    }
];

db.collection.aggregate(pipeline);

The Spring Data MongoDB equivalent follows:

Criteria dateCriteria = new Criteria().andOperator(Criteria.where("myDate").gte(startDate).lte(endDate),
                                                   Criteria.where("dateDifference").gt(0));
Aggregation agg = Aggregation.newAggregation(       
    project("id", "status", "studentId", "myDate") 
        .andExpression("currDate - myDate").as("dateDifference"),
        //.and(currDate).minus("myDate").as("dateDifference"), <-- or use expressions
    match(Criteria.where("studentId").is("100").andOperator(dateCriteria)),
    group("status"), 
        .count().as("activeCount")
);
chridam
  • 100,957
  • 23
  • 236
  • 235
  • Still its not working, the rows returned from the query is ZERO – chiku Nov 05 '15 at 10:52
  • FYI the currentDate is not of type java.util.Date, currDate is a another field in a collection. – chiku Nov 05 '15 at 17:27
  • its not working, the rows returned from the query is ZERO. Pls lemme know how to compare two date fields in the where clause in spring-data-mongodb? – chiku Nov 05 '15 at 18:14
  • Pls refer this link http://stackoverflow.com/questions/20800069/mongodb-spring-data-comparison-between-fields , query pls – chiku Nov 05 '15 at 18:34