0

I am using firestore to store my data and I want to fetch a collection of users from my db except one (Current User). How can I do this.

This is the image of my db

enter image description here

Here I want to fetch all users except the one with id 'gi3oGztVrKQoiEHxFv28TAQpxah2'

The code I am currently using is

this.donorCollection = this.fireStore.collection<any>('users').valueChanges(); console.log(this.donorCollection); this.donorCollection.subscribe(data => console.log(data) );

Which returns all users. The reason for this specific task is that my current user always updates his/her db whenever his/her position changes. Therefore if I applied valuechanges() to the current collection it will always give new value as current user is updating his/her postion.

Dijish U.K
  • 159
  • 1
  • 22

3 Answers3

1

You can't exclude documents with a Firestore query. With Firestore queries, you specify all the conditions that must be true (where x and y and z, etc).

That said, it's trivial and inexpensive to filter the results of the query on the client if all you want to do is exclude one document.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
0

Just filter from the collection as follows,

this.donorCollection.subscribe((data) => {
 this.users = data.filter(person => person.id != 'gi3oGztVrKQoiEHxFv28TAQpxah2');
 console.log(this.users) ;
});
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
0

I was able to solve this by refering this -

Queries with a != clause. In this case, you should split the query into a greater-than query and a less-than query. For example, although the query clause where("age", "!=", "30") is not supported, you can get the same result set by combining two queries, one with the clause where("age", "<", "30") and one with the clause where("age", ">", 30).

Since my phone field is unique . I solved this by calling query as follows

this.donorCollectionLR = this.fireStore.collection<any>('users', ref =>
      ref.where('phone', '<', '8086960113')).valueChanges();
      this.donorCollectionLR.subscribe(data =>{
        this.donorCollectionGR = this.fireStore.collection<any>('users', ref =>
        ref.where('phone', '>', '8086960113')).valueChanges();
        this.donorCollectionGR.subscribe(data2 =>{
          data2=data2.concat(data);
          observer.next(data2)
        });
      });
Dijish U.K
  • 159
  • 1
  • 22