wiesson
wiesson

Reputation: 6822

Query Firestore Rest API by multiple document ids

I have noticed that documents that are returned by the API / runQuery do contain the document name, but I can't find any information within the structuredQuery docs how to query by multiple ids.

{
  document: {
    name: 'projects/:projectId/databases/(default)/documents/collection/id',
    fields: ...,
    createTime: '2021-01-16T13:35:02.151442Z',
    updateTime: '2021-01-18T10:42:32.257199Z'
  },
  readTime: '2021-01-18T17:10:33.600112Z'
},

Firestore does support a whereIn query by document id Collection.where(firestore.FieldPath.documentId(), 'in', ["123","456","789"]), so I thought the Rest API might support is as well.

Do I miss something, or does runQuery not support queryById functionality?

Upvotes: 1

Views: 410

Answers (1)

Soni Sol
Soni Sol

Reputation: 2612

This can be done by calling batchGet endpoint:

https://content-firestore.googleapis.com/v1beta1/projects/<PROJECT_ID>/databases/(default)/documents:batchGet

with the following JSON on the request body.

{
  "documents": [
    "projects/<PROJECT_ID>/databases/(default)/documents/<COLLECTION_ID>/<DOC_ID1>",
    "projects/<PROJECT_ID>/databases/(default)/documents/<COLLECTION_ID>/<DOC_ID2>",
    "projects/<PROJECT_ID>/databases/(default)/documents/<COLLECTION_ID>/<DOC_ID3>",
  ]
}

Upvotes: 3

Related Questions