Search code examples
mongodbspring-dataspring-mongospring-mongodb

How to get collection fields with one where condition and a date range using Spring mongoTemplate?


I want to translate following console mongodb query into Spring mongoTemplate query

db.transaction.find({"account_number":10}, {"entry_date":{"$gte": new ISODate("2016-07-20"),"$lte": new ISODate("2016-07-21")}}).count()

I have tried following

 Query query = new Query();
        Criteria criteria =
                Criteria.where("account_number").is(accountNumber).and("account_code").is(accountCode)
                .and("entry_date").gte(start).lte(end);
        query.addCriteria(criteria);
        mongoTemplate.find(query, Transaction.class, "transaction");
        return mongoTemplate.find(query, Transaction.class, "transaction");

When debugging, I got above code translated into following query value. The query is not returning same results I got on mongo console.

Query: { "account_number" : 10 , "account_code" : 2102 , "entry_date" : { "$gte" : "2015-05-20" , "$lte" : "2016-07-21"}}

The question is how I can build a query to pass two different conditions with find like I did on console ?

What I am trying to achieve is get all transactions between two dates (inclusive) for a given account_number.

Collection Structure

    {
    "_id" : ObjectId("5825e49585a4caf2bfa30ff4"),
    "profit" : "",
    "account_number" : 280,
    "m_number" : "",
    "registration_number" : "",
    "page_number" : "",
    "entry_date" : ISODate("2014-10-20T07:33:57Z"),
    "narration" : "To cash",
    "voucher_number" : "",
    "debit_credit" : -4400,
    "account_code" : 2105,
    "created_at" : ISODate("2014-10-20T07:33:57Z"),
    "updated_at" : ISODate("2014-10-20T07:33:57Z"),
    "employee_id" : 0,
    "balance" : 0,
    "credit" : 0,
    "debit" : 0,
    "particulars" : "",
    "since_last" : 0,
    "transaction_type" : 0,
    "voucher_path" : "",
    "branch" : "",
    "auto_voucher_number" : "",
    "check_book_series" : ""
}
{
    "_id" : ObjectId("5825e49585a4caf2bfa30ff5"),
    "profit" : "",
    "account_number" : 1555,
    "m_number" : "",
    "registration_number" : "",
    "page_number" : "",
    "entry_date" : ISODate("2014-10-20T07:33:57Z"),
    "narration" : "To",
    "voucher_number" : 73804,
    "debit_credit" : -1550,
    "account_code" : 2101,
    "created_at" : ISODate("2014-10-20T07:33:57Z"),
    "updated_at" : ISODate("2014-10-20T07:33:57Z"),
    "employee_id" : 0,
    "balance" : 0,
    "credit" : 0,
    "debit" : 0,
    "particulars" : "",
    "since_last" : 0,
    "transaction_type" : 0,
    "voucher_path" : "",
    "branch" : "",
    "auto_voucher_number" : "",
    "check_book_series" : ""
}

Solution The original query is only returning records for account_number=10 and ignores entry_date condition. I guess it is wrong way of querying. Here is the correct solution

   DateTime start, DateTime end;

        Query query = new Query();
        Criteria criteria =
                Criteria.where("account_number").is(accountNumber).and("account_code").is(accountCode)
                .and("entry_date").gte(start).lte(end);
        query.addCriteria(criteria);

        return mongoTemplate.find(query, Transaction.class, "transaction");

it is important that the dates are in correct format. E.g java.util.Date or JODA DateTime


Solution

  • You are passing the dates as String. Try passing both the start date and end date as java.util.Date types. For example

    LocalDate startLocalDate = LocalDate.of(2014, 10, 19);
    Date startDate = Date.from(startLocalDate.atStartOfDay().toInstant(ZoneOffset.UTC));
    
    Criteria criteria = Criteria.where("entry_date").gte(startDate);