最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - unable to query date range in mongo repository in spring boot - Stack Overflow

programmeradmin0浏览0评论

Mongo document has a field createdOn which stores ISODate i.e. java.util.Date in mongo document

I am trying to get count on date range fields. When I give date range from and to from restclient I am getting count as 0, with an exception saying :

org.bson.json.JsonParseException: JSON reader expected a string but found '?0'.

below is the query I am using in repository

   public interface PaymentMongoLogDetailRepository extends MongoRepository<PaymentMongoLogDetail, String> {
    @Query(value = "{'$and' : [{'createdOn':{'$gt':ISODate(?0)}},{'createdOn':{'$lte':ISODate(?1)}},{'stripeResponse.sessionId':/cs_live/},{'stripeResponse.status':'success'}]}",count = true)
    Long findAllSuccessCounts(String from,String to);
}

Place holders ?0 and ?1 are for String from and String to respectively which would have date format yyyy-MM-dd

Same query when I am running in mongo shell is giving me non zero counts.

db.paymentMongoLogDetail.find({'$and' : [{'createdOn':{'$gt':ISODate('2025-01-24')}},{'createdOn':{'$lte':ISODate('2025-02-08')}},{'stripeResponse.sessionId':/cs_live/},{'stripeResponse.status':'success'}]}).count();

above query is fetching me non zero count.

But mongorepository is giving me exception message :

org.bson.json.JsonParseException: JSON reader expected a string but found '?0'.

What should I change to get the accurate counts here.

UPDATE

Below code worked for me

public interface PaymentMongoLogDetailRepository extends MongoRepository<PaymentMongoLogDetail, String> {
    @Query(value = "{'$and' : [{'createdOn':{'$gt': ?0,'$lte': ?1}},{'stripeResponse.sessionId':/cs_live/},{'stripeResponse.status':'success'}]}",count = true)
    Long findAllSuccessCounts(Date from,Date to);
}

Mongo document has a field createdOn which stores ISODate i.e. java.util.Date in mongo document

I am trying to get count on date range fields. When I give date range from and to from restclient I am getting count as 0, with an exception saying :

org.bson.json.JsonParseException: JSON reader expected a string but found '?0'.

below is the query I am using in repository

   public interface PaymentMongoLogDetailRepository extends MongoRepository<PaymentMongoLogDetail, String> {
    @Query(value = "{'$and' : [{'createdOn':{'$gt':ISODate(?0)}},{'createdOn':{'$lte':ISODate(?1)}},{'stripeResponse.sessionId':/cs_live/},{'stripeResponse.status':'success'}]}",count = true)
    Long findAllSuccessCounts(String from,String to);
}

Place holders ?0 and ?1 are for String from and String to respectively which would have date format yyyy-MM-dd

Same query when I am running in mongo shell is giving me non zero counts.

db.paymentMongoLogDetail.find({'$and' : [{'createdOn':{'$gt':ISODate('2025-01-24')}},{'createdOn':{'$lte':ISODate('2025-02-08')}},{'stripeResponse.sessionId':/cs_live/},{'stripeResponse.status':'success'}]}).count();

above query is fetching me non zero count.

But mongorepository is giving me exception message :

org.bson.json.JsonParseException: JSON reader expected a string but found '?0'.

What should I change to get the accurate counts here.

UPDATE

Below code worked for me

public interface PaymentMongoLogDetailRepository extends MongoRepository<PaymentMongoLogDetail, String> {
    @Query(value = "{'$and' : [{'createdOn':{'$gt': ?0,'$lte': ?1}},{'stripeResponse.sessionId':/cs_live/},{'stripeResponse.status':'success'}]}",count = true)
    Long findAllSuccessCounts(Date from,Date to);
}
Share Improve this question edited yesterday JPG asked yesterday JPGJPG 1,2535 gold badges32 silver badges69 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

I removed unnecessary ISODate and below code worked for me. I clubbed the $gt and $lte in same condition of $and operator. Also changed String with Date in parameter.

public interface PaymentMongoLogDetailRepository extends MongoRepository<PaymentMongoLogDetail, String> {
    @Query(value = "{'$and' : [{'createdOn':{'$gt': ?0,'$lte': ?1}},{'stripeResponse.sessionId':/cs_live/},{'stripeResponse.status':'success'}]}",count = true)
    Long findAllSuccessCounts(Date from,Date to);
}

You can solve this using jpa method naming nomenclature like findByCreatedOnBetween (something like that) but I am more comfortable with native queries of mongo.

发布评论

评论列表(0)

  1. 暂无评论