Search code examples
angulartypescriptangularfire

AngularFIre: LImit to 10 most recents registers


I have a query on firebase, but I want to show just the most 10 recents registers instead all of them

my code is:

  videos: Observable<any[]>;

  constructor(firestore: AngularFirestore,) {

    this.videos = firestore.collection('videos').valueChanges();

  }

Someone know the best way to do that?

Thanks!


Solution

  • You can pipe your observable and slice the 10 recent items using map operator from 'rxjs', something like

      videos: Observable<any[]>;
    
      constructor(firestore: AngularFirestore,) {
    
        this.videos = firestore.collection('videos').valueChanges().pipe(
          map(videos => videos.slice((videos.length - 10), videos.length))
        )
    
      }