Glasnhost
Glasnhost

Reputation: 1135

spring mongoRepository find by time

I have the following query working in mongoDB

  db.item.find({"startdate":{"$gte" : (new Date(itemDate)).getTime()}})

where itemDate is a date in YYYY-MM-DD format

  itemDate="2017-01-01"

Now I want to write it in a springBoot application, in a class extending mongoRepository.

I tried trivially:

   `itemRepository.find({"itemDate":{"$gte" : (new Date(itemDate)).getTime()}})`;

But I can't even compile it. I don't get which method of mongoRepository to use. The field startdate seems to be stored as timestamp.

Upvotes: 0

Views: 219

Answers (1)

Steven Diamante
Steven Diamante

Reputation: 217

MongoTemplate may suit your needs better. It is better to use when there are more complex queries like this one. With MongoTemplate you would write that query like this:

Query query = Query.query(Criteria.where("startdate").gte(new Date(itemDate).getTime()));
mongoTemplate.find(query, Item.class);

Upvotes: 1

Related Questions