我试图查询按数组值排序的mongoDB集合(除了其他字段),并且结果也需要分页。排序不适用于数组值,但适用于其他字段。这是代码:
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);
非常感谢您的帮助。
答案 0 :(得分: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);
sort参数是value
(字符串类型),在其上执行排序,您不能传递需要进一步评估(根据您的情况)的字符串以获取值。而是直接将您的值保存到sortBy
变量中,然后将sortBy
传递给查询。
答案 1 :(得分:0)
此表达式成功运行 sortBy =“ programInstance.title.descriptions.0.value”;