Reputation: 533
I am trying to query a mongoDB collection sorting by an array value(in addition to other fields) and the results need to be paginated also. The sorting is not working for the array value, but it works for other fields. Here is the code:
sortBy = "programInstance.title.descriptions[0].value";
PageRequest pageRequest = = PageRequest.of(filter.getPageNumber(), filter.getPageSize(),
new Sort("DESC".equalsIgnoreCase(filter.getSortOrder()) ? Sort.Direction.DESC : Sort.Direction.ASC, sortBy));
Page<Offer> st = new PageImpl<>(mongoTemplate.find(query.with(pageRequest),Offer.class), pageRequest, pageCount);
Any help is very much appreciated..
Upvotes: 2
Views: 647
Reputation: 533
It worked successfully with this expression sortBy = "programInstance.title.descriptions.0.value";
Upvotes: 1
Reputation: 1821
//One or two records from your collection
const data = [{
name: 'abc',
place: 'US',
zone: 'Pacific'
}, {
name: 'xyz',
place: 'PK',
zone: 'Asia'
}, ]
const fields = Object.keys(data[0]);
console.log("Fields in collection: ",fields);
const sortBy = fields[0];
console.log("Sort By: ", sortBy);
//const sort = new Sort("DESC".equalsIgnoreCase(filter.getSortOrder()) ? //Sort.Direction.DESC : Sort.Direction.ASC, sortBy);
//PageRequest pageRequest = = PageRequest.of(filter.getPageNumber(), //filter.getPageSize(), sort);
//Page < Offer > st = new PageImpl < > //(mongoTemplate.find(query.with(pageRequest), Offer.class), pageRequest, //pageCount);
The sort parameter is a value
(string type), on which the sort is performed, you can't pass a string which needs further evaluation (in your case) to get the value. Instead directly save your value into your sortBy
variable and then pass sortBy
to your query.
Upvotes: 0