简体   繁体   English

查询MongoDB按数组值排序并在Springdata MongoDB中使用分页

[英]Query mongoDB sorting by array value and using pagination in Springdata MongoDB

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. 我正在尝试查询按数组值排序的mongoDB集合(除了其他字段),并且结果也需要分页。 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.. 很感谢任何形式的帮助..

 //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. sort参数是一个value (字符串类型),在其上执行排序,您不能传递需要进一步评估的字符串(在您的情况下)才能获取该值。 Instead directly save your value into your sortBy variable and then pass sortBy to your query. 而是直接将您的值保存到您的sortBy变量中,然后将sortBy传递给您的查询。

它成功使用此表达式sortBy =“ programInstance.title.descriptions.0.value”;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM