2

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..

3
  • 1
    your soryBy is a string, where you are trying to get a value from 0 index. Instead of this save the value directly in sortBy. Sort takes a string parameter to sort, it does not evaluate the string. Commented Feb 13, 2019 at 6:00
  • 1
    @its4zahoor : sorry, I did not understand what you meant. Can you tell me an example? Did u mean query the database for this value and then pass it to Sort? Commented Feb 13, 2019 at 6:04
  • 1
    yeah , Query the database for this value, then pass it to sort. In my answer i assumed you have a local array. Commented Feb 13, 2019 at 6:13

2 Answers 2

1

It worked successfully with this expression sortBy = "programInstance.title.descriptions.0.value";

Sign up to request clarification or add additional context in comments.

Comments

0

//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.

4 Comments

Maybe I understood wrongly, if I query the database for this, I will get the value of the field, but not the field name, won't I? A field name is what we need to pass to the Sort?
programInstance.title.descriptions[0].value in your code, will return the value. if you are interested in the field name see how to get fields from collection docs.mongodb.com/v3.2/tutorial/…
javascript way is to store few records of data/collection in array and use var field = Object.keys(array) to get all fields. then use field[index] to get the field.
I have updated my answer to show how to get keys(fields), I commented the last part as it not required(but there for reference). Run the snippet and see the console.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.