Find Results within Date range Using Mongo NamedQuery - mongodb

I want to find the Results based on Mongo NamedQuery
#Document(collection="test")
#Getter
#Setter
public class TestData {
#Id
private String name;
private String fiscalWeek;
private String fiscalYear;
}
Here fisaclWeek in the range of 1 to 52
#Query("'fiscalWeek' : {$gt : ?0, $lt : ?1}}")
List<TestData> findByDateRange(String fromDate, String toDate);
here fromDate and toDate will come like this 512015 that means concatenation of week and year
I tried like the above way but need to concatenate fisaclWeek and fisaclYear but not sure how to concatenate in the #Query method
How to write a Query here so that I will get results in the range of fromDate and toDate?
Can anyone help on this.

Related

How to get distinct Items by one field in MongoDB using #Query annotation with pagination enabled

I'm trying to get the list of distinct items by a specific field (userId). Currently I'm using this kind of approach to get records from MongoDB using ReactiveCrudRepository. Additionally, I want this result to be further filtered and get only the distinct items.
How to do this?
#Query(value = "{$and :[{'submitTime':{$ne:null}}, {'gameId': :#{#gameId}} ]}", sort = "{'score': -1, 'timeTaken': 1, 'submitTime': 1}")
Flux<Play> getWinners(#Param("gameId") String gameId, Pageable pageable);
My Play object is like this:
#Document(value = "play")
#Builder
public class Play {
#Id
private String id;
private String gameId;
private int score;
private String userId;
private LocalDateTime submitTime;
private long timeTaken;
}
At the moment #Query annotation doesn't provide a possibility to execute the distinct command.
As a workaround you can
implement a custom method with find, sort and distinct actions
use .distinct() operator after calling your repo method getWinners() to distinct items by key selector, but in this case this operation will not be performed in MongoDB

Spring Data JPA sort by calculated fields

I have an entity which contains the data for the run - date, distance, duration and some additional:
#Entity
class RunEntry {
private Long id;
private LocalDate date;
private int weekNo;
private Long distance;
private Duration duration;
private Long userId;
// getters, setters
}
Based on this data I have to create a report which will contain per week distance and average speed (that's why I added weekNo column to easily group by):
class AverageWeekRun {
private LocalDate weekStart;
private LocalDate weekEnd;
private BigDecimal averageSpeed;
private Long distance;
// getters, setters
}
To achieve this I create a Spring Data JPA projection which contains weekNo, sum of distance and sum of duration:
interface RunPerWeek {
int getWeekNo();
Long getSumOfDistance();
Long getSumOfDuration();
}
and I created a simple method in repository:
#Query("select r.weekNo as weekNo, sum(r.distance) as sumOfDistance, sum(r.duration) as sumOfDuration "
+ "from RunEntry r where r.userId = :userId group by r.weekNo")
Page<RunPerWeek> findRunPerWeekForUser(Long userId, Pageable pageable)
It works fine, I can use this query result and map it to AverageWeekRun (by calculating the week start/end based on weekNo and average speed based on sum of distance and sum of duration).
However, I want to use sort from pageable to order by average speed or by week start or week end date. These fields are calculate so I get an exception when I try to pass it with sort. How can it be solved? I want the user to use field names from AverageWeekRun class but some of them are calculated and Spring Data JPA cannot handle it.
You can use:
Sort sort = Sort.by(Sort.Direction.DESC, "sumOfDistance");
Pageable pageable = PageRequest.of(page, limit, sort);
I have used it, as long as your calculated value has "as somename", this will work.

#Id of type date for SpringData Mongo

The data pojo for spring-data-mongodb is configured as follows:
#Document(collection="DateKey")
public class DateKey {
#Id
private Date dateid;
private Date dateval;
private String firstName;
private String lastName;
The #Id column is a java.util.Date field, when the data is saved in mongo-db using spring-data-mongodb, it is getting saved as
{
"_id" : ObjectId("5a356628d73a971c3c8ab1e7")
}
Whenever other datatypes are used, no such behavior is observed. Due to this when we try to find the value from database, it throws the following exception
Caused by: org.springframework.core.convert.ConverterNotFoundException: No
converter found capable of converting from type [org.bson.types.ObjectId] to
type [java.util.Date]
only java.sql.Date is saved as Date in MongoDB, an ordinary java.util.Date is not!
Must provide a custom converter

Spring Data save date and time in specific format

I don't understand how can I save date and time for example "eventDateFrom" in MongoDB in specific format.
I try do it like that:
Part of Event class:
public class Event {
#Id
private String id;
private String eventTitle;
#DateTimeFormat(pattern = "MM/dd/yyyy HH:mm:ss")
private Date eventDateFrom;
private String eventDateTo;
private Integer minEventPartcipants;
private String eventDateConfirmTo;
private String location;
private String minEventTime;
private List<Participant> participantList = new ArrayList<>();
Part of RunAtStart class:
Event event = new Event();
event.setEventTitle("Wyjazd na rower Turbacz");
event.setEventDateFrom(new Date("05/27/2017 10:00:00"));
event.setEventDateTo("27.05.2017 22:00:00");
event.setMinEventPartcipants(2);
event.setEventDateConfirmTo("26.05.2017 10:00:00");
event.setLocation("Lindego 13a");
event.setMinEventTime("06:00:00");
But the time "eventDateFrom" is wrong.
MongoDB document:
{
"_id" : ObjectId("592d34dcc5f2fd2d204d7db4"),
"_class" : "info.wzielezicki.app.MeetHelp.model.Event",
"eventTitle" : "Wyjazd na rower Turbacz",
"eventDateFrom" : ISODate("2017-05-27T08:00:00.000Z"),
"eventDateTo" : "27.05.2017 22:00:00",
"minEventPartcipants" : 2,
"eventDateConfirmTo" : "26.05.2017 10:00:00",
"location" : "Lindego 13a",
"minEventTime" : "06:00:00",
"participantList" : []
}
What I'm doing wrong?
Is the better way to define date and time format?
If you want to store a Date in a specific format and not the one, the store uses, you don't really want to store a Date, but a String, so just make the property of type String. You can still have additional accessors that use Date to use inside your application.

How can I query to find mongo entities whose list of sub-entities contain a field matching a string?

I have a collection of entities that look like this:
public class ClientEntity {
#Id
private String id;
#Indexed(unique = true)
private String clientId;
private String name;
#DBRef
private List<ClientMachineEntity> machines;
...
}
...where ClientMachineEntity looks like:
public class ClientMachineEntity {
#Id
private String id;
#Indexed(unique = true)
private String clientMachineId;
private String hostName;
...
}
I have a working search that finds ClientEntities by matching against "clientId" and "name":
public List<ClientEntity> searchByIdAndName(String id, String name) {
Criteria idCriteria = Criteria.where("clientId").regex(id, "i");
Criteria nameCriteria = Criteria.where("name").regex(name, "i");
Query query = new Query(new Criteria().orOperator(idCriteria, nameCriteria));
...
}
So my question is, how can I expand this search so that it also matches against "clientMachineId" in the list of sub-entities? I tried adding the following criteria:
Criteria machineCriteria = Criteria.where("machines.clientMachineId").regex(id, "i");
...but that doesn't work, presumably because machines is a LIST of entities, not just a single sub-entity.
UPDATE: It seems like what I'm looking for is the .elemMatch() functionality, but when I try that:
Criteria machineCriteria = Criteria.where("machines").elemMatch(Criteria.where("clientMachineId").regex(id, "i"));
...I get the following error:
org.springframework.data.mapping.model.MappingException: No mapping metadata found for class com.mongodb.BasicDBObject
You can't query by fields in subentities linked with DBRef. If ClientMachineEntity would be embedded in ClientMachine - then you could use dot notation or $elemMatch depending on needs.
In your particular example - couldn't field ClientMachineEntity.clientMachineId be saved as _id and used as a primary key? Then you could get the results you need - take a look at: How to query mongodb with DBRef
My suggestion for development with Spring Data MongoDB is - first learn how to (and if it's possible) do it in plain Javascript with MongoDB console, then learn how to do the same with Spring Data MongoDB.