google-cloud-go icon indicating copy to clipboard operation
google-cloud-go copied to clipboard

Firestore: Support passing a slice of strings when querying `firestore.DocumentID IN ["docId, "docId2"]`

Open JasperSurmont opened this issue 3 years ago • 1 comments

Client

FireStore

Environment

Win10

Go Environment go version: go1.16.3 windows/amd64

Code

e.g.

iterator := someCollection.Where(firestore.DocumentID, "in", someSlice).Documents(context.Background())

Expected behavior

Iterator contains all the snapshots of documents of which their ID is present in the someSlice slice

Actual behavior

Error is thrown: rpc error: code = InvalidArgument desc = __key__ filter value must be a Key

Additional context

This should work following the answers on this thread

JasperSurmont avatar May 23 '21 18:05 JasperSurmont

Thanks for the issue. I validated this should work, it is particular however.

The type of the elements in someSlice is important. If it is a document reference it should work, however, passing the id as a string will not work as authored today.

So, for instance, the following would work:

docs := []*firestore.DocumentRef{collection.Doc("id")}
iter := collection.Where(firestore.DocumentID, "in", docs).Documents(context.Background())

I agree it should for ease of use and will consider this a feature request.

In toProto, where filters are processed, the fieldPath set to documentID should check on in queries if the values are strings. If so, those strings could be adjusted to be documentRefs.

Node.js handles this today by validating the reference and as part of it doing a conversion. It would be convenient for go users if a similar conversion were done. Referenced Node Code: https://github.com/googleapis/nodejs-firestore/blob/9a1a9dcd56dc125159be3ce7356a6ebce04519a9/dev/src/reference.ts#L1646

crwilcox avatar Aug 12 '21 19:08 crwilcox