[flutter] Firebase 쿼리 기능을 사용하여 데이터 정렬 및 제한 방법은?
Firebase Firestore에서는 데이터베이스 쿼리를 실행하여 결과를 필터링, 정렬 및 제한할 수 있습니다. 아래는 데이터 정렬과 제한에 관련된 몇 가지 기능에 대한 설명입니다.
데이터 정렬
데이터를 특정 필드를 기준으로 정렬하려면 orderBy
메서드를 사용합니다.
QuerySnapshot querySnapshot = await FirebaseFirestore.instance
.collection('cities')
.orderBy('name')
.get();
위의 예제에서는 cities
컬렉션의 데이터를 name
필드를 기준으로 정렬한 결과를 가져옵니다.
데이터 제한
데이터를 필요한 만큼 제한하려면 limit
메서드를 사용합니다.
QuerySnapshot querySnapshot = await FirebaseFirestore.instance
.collection('cities')
.orderBy('name')
.limit(5)
.get();
위의 예제에서는 cities
컬렉션의 데이터를 name
필드를 기준으로 정렬한 후 상위 5개의 결과를 가져옵니다.
조합해서 사용
orderBy
와 limit
메서드를 조합하여 데이터를 필터링하고 정렬할 수도 있습니다.
QuerySnapshot querySnapshot = await FirebaseFirestore.instance
.collection('cities')
.where('population', isGreaterThan: 1000000)
.orderBy('name')
.limit(3)
.get();
위의 예제에서는 population
이 100만 이상인 데이터를 필터링하고 name
필드를 기준으로 정렬한 후 상위 3개의 결과를 가져옵니다.
Firebase Firestore를 사용하여 데이터를 쿼리하고 정렬하는 방법에 대해 간단히 살펴보았습니다. Firebase 문서를 참조하여 더 자세한 정보를 확인할 수 있습니다.