and*_*dyb 3 java spring mongodb spring-data
我想find使用mongo JSON查询对结果进行排序并完成一些阅读和实验,我仍然无法使其工作.我有PagingAndSortingRepository,可以Sort()在findAll上使用没有问题.
public interface ThingRepository extends PagingAndSortingRepository<Thing, ObjectId> {
@org.springframework.data.mongodb.repository.Query("{ name:?0, $or : [ { state:'new' } , {state:'updated'} ] }")
List<Device> findThingsInNewOrUpdatedState(String name);
}
Run Code Online (Sandbox Code Playgroud)
@Service
public class ThingService() {
@Autowired private ThingRepository thingRepository;
public List<Thing> getSortedThings() {
return (List<Thing>)thingRepository.findAll(new Sort(Sort.Direction.DESC, Arrays.asList("dateModified")));
}
public List<Thing> getNewOrUpdatedThingsSorted() {
return thingRepository.findThingsInNewOrUpdatedState(); // <-- this needs to be sorted
}
}
Run Code Online (Sandbox Code Playgroud)
查询直接转换为mongoDb调用,工作正常
db.things.find({ name:'xxx', $or : [ { state:'new' }, {state:'updated'} ] })
Run Code Online (Sandbox Code Playgroud)
我知道我可以添加sort()常规的mongoDb语法,但无法解决如何在Java/Spring Data中执行此操作.它试图将它添加到@Query但它没有用,因为我认为Spring只是执行一个find().
您应该能够简单地Sort向查询方法添加一个参数,从而动态管道Sort将应用于定义的查询的实例@Query.
| 归档时间: |
|
| 查看次数: |
7600 次 |
| 最近记录: |