Tapas Mukherjee
Tapas Mukherjee

Reputation: 2178

Check multiple Document exists id Firestore Collection

I want check within a list of 10 document IDs how many exists in a collection. One way to do it would store the document ID inside the document and use in operator.

const result = await this.afs.collection(path).where('id', 'in', ['doc1', 'doc2']).get();

Is there a way we can avoid storing the document id inside document.

Upvotes: 0

Views: 264

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317392

For this particular query, you can use FieldPath.documentId(). It returns a token that you can use in the field path of the query:

this.afs.collection(path).where(FieldPath.documentId(), 'in', ['doc1', 'doc2'])

I've linked you to the plain JavaScript documentation. If you're using Angular, it might have a slightly different way of getting this token value, but it will be in a class called FieldPath.

Upvotes: 1

Related Questions