7

When I query data from Firebase Firestore with documentId as field path, I get a different behaviour when running the script on webpage (javascript) and in Firebase Function (Node.js).

This javascript gives me perfect results:

firebase.firestore().collection('test')
  .where(firebase.firestore.FieldPath.documentId(), '<=', 'ccc')
  .get()
  .then(snapshots => { /* results are here */ });

by contrast the same code in Firebase Function (Node.js):

admin.firestore().collection('test')
  .where(admin.firestore.FieldPath.documentId(), '<=', 'ccc')
  .get()
  .then(snapshots => { /* ... */ });

gives me a error:

Error: { Error: a filter on __name__ must be a document resource name at ClientReadableStream._emitStatusIfDone (/user_code/node_modules/firebase-admin/node_modules/grpc/src/client.js:255:19) at ClientReadableStream._receiveStatus (/user_code/node_modules/firebase-admin/node_modules/grpc/src/client.js:233:8) at /user_code/node_modules/firebase-admin/node_modules/grpc/src/client.js:705:12 code: 3, metadata: Metadata { _internal_repr: {} } }

I use my own document ids and I want to query by these ids. I know I can walk around this problem by querying by some document's inner field, but my question: what is the reason for this different behaviour? Thanks a lot.

My firebase version is 3.17.4

Edit: This bug was solved and does not appear in Firebase version 3.18.2.

zelig74
  • 482
  • 1
  • 6
  • 15

1 Answers1

10

This is indeed a feature omission from the Node SDK, which we will address in the next release.

You can work around this for now by directly passing a DocumentReference as such:

const coll = admin.firestore().collection('test')
coll
  .where(admin.firestore.FieldPath.documentId(), '<=', coll.doc('ccc'))
  .get()
  .then(snapshots => { /* ... */ });
  • @google-cloud/firestore v0.12.0 includes a fix for this issue. – Sebastian Schmidt Feb 26 '18 at 21:51
  • 2
    sorry to update this thread but I was unable to achieve a where using documentId FieldPath. Always return the "The corresponding value for FieldPath.documentId() must be a string or a DocumentReference" error :/ With either a string or a doc (using `in` with an array) – Hugo Gresse Dec 27 '19 at 11:01
  • 1
    I'm still arguing this with the firestore team. The net result is FieldPath.documentId is *not* matching on the documentId, but on the refPath (which it gets automatically if passed a document reference). If you *have* the document reference, why are you using the collectionGroup at *all*? – LeadDreamer Jan 17 '20 at 23:50