3

mongo JSON クエリfindを使用して結果を並べ替えたいのですが、いくつかの読み取りと実験を行った後も、まだ機能しません。私はPagingAndSortingRepositoryを持っており、findAllで問題なく使用できます。Sort()

リポジトリ クラス

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);
}

サービス層

@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
    }
}

クエリは、正常に動作する mongoDb 呼び出しに直接変換されます。

db.things.find({ name:'xxx', $or : [ { state:'new' }, {state:'updated'} ] })

通常の mongoDb 構文でa を追加できることはわかっていsort()ますが、Java/Spring Data 内からこれを行う方法がわかりません。それを @Query に追加しようとしましたが、Spring はfind().

4

1 に答える 1

5

You should be able to simply add a Sort parameter to the query method and thus dynamically pipe in Sort instances that will be applied to the query defined in @Query.

于 2011-10-13T17:46:40.110 に答える