0

I have records in my mongodb which are like this example record.

{
"_id" : ObjectId("5de6e329bf96cb3f8d253163"),
"changedOn" : ISODate("2019-12-03T22:35:21.126Z"),
"bappid" : "BAPP0131337",   
}

I have code which is implemented as:

public List<ChangeEvent> fetchChangeList(Application app, Date from, Date to) {
    Criteria criteria = null;
    criteria = Criteria.where("bappid").is(app.getBappid());
    Query query = Query.query(criteria);
    if(from != null && to == null) {
        criteria = Criteria.where("changedOn").gte(from);
        query.addCriteria(criteria);
    }
    else if(to != null && from == null) {
        criteria = Criteria.where("changedOn").lte(to);
        query.addCriteria(criteria);
    } else if(from != null && to != null) {
        criteria = Criteria.where("changedOn").gte(from).lte(to);
        query.addCriteria(criteria);
    }
    logger.info("Find change list query: {}", query.toString());

    List<ChangeEvent> result = mongoOps.find(query, ChangeEvent.class);
    return result;

This code always comes up empty. The logging statement generates a log entry like:

Find change list query: Query: { "bappid" : "BAPP0131337", "changedOn" : { "$gte" : { "$date" : 1575418473670 } } }, Fields: { }, Sort: { }

Playing around with variants of the query above in a database which has the record above gets the following results we get.

Returns records:

db["change-events"].find({ "bappid" : "BAPP0131337" }).pretty();

Returns empty set:

db["change-events"].find({ "bappid" : "BAPP0131337", "changedOn" : { "$gte" : { "$date" : 1575418473670 } } }).pretty();

Returns empty set:

db["change-events"].find({ "bappid" : "BAPP0131337", "changedOn" : { "$lte" : { "$date" : 1575418473670 } } }).pretty();

The record returned without the date query should be non empty on one of the two above. But it is empty on both.

What is wrong here?

krishna Prasad
  • 3,541
  • 1
  • 34
  • 44
Steve Owens
  • 851
  • 1
  • 5
  • 10
  • Does your collection name `change-events` correct? and also annotated with `@Document("change-events")` in the model class `ChangeEvent`? – krishna Prasad Dec 04 '19 at 02:07

1 Answers1

0

Since the collection name change-events is different then Class name ChangeEvent so you have to pass the collection name in the find query of mongoOps as below:

List<ChangeEvent> result = mongoOps.find(query, ChangeEvent.class, "change-events");

I have tried it replicating and found that your query without dates in where clause also not working i.e:

Criteria criteria = null;
criteria = Criteria.where("bappid").is(bappid);
Query query = Query.query(criteria); 

And the find query on mongoOps as below:

List<ChangeEvent> result = mongoTemplate.find(query, ChangeEvent.class);

Will not work, becuase collection name is missing, below query with collection name execute fine:

List<ChangeEvent> result1 = mongoTemplate.find(query, ChangeEvent.class, "changeEvents");

For details explanation of above discussion you can find out at my Github repo: https://github.com/krishnaiitd/learningJava/blob/master/spring-boot-sample-data-mongodb/src/main/java/sample/data/mongo/main/Application.java#L157

krishna Prasad
  • 3,541
  • 1
  • 34
  • 44