Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

collectionGroup() and query with FieldPath #587

Closed
Cdik opened this issue Jul 3, 2019 · 9 comments
Closed

collectionGroup() and query with FieldPath #587

Cdik opened this issue Jul 3, 2019 · 9 comments
Assignees

Comments

@Cdik
Copy link

Cdik commented Jul 3, 2019

Hi all,

Sorry if this issue shouldn't be posted here. I can't find the right place to post it.

is there a good reason why I can't query a collectionGroup() with FieldPath.documentId()??

It's working fine if a query on a specific collection but when I try querying from collectionGroup I get

Error: Resource name "projects/my-project-name/databases/(default)/documents/oOuEOw2BFeqypiJmEDuj" lacks "/" at index 80."

and of course if I try adding a '/' at the end of the FieldPath, I get

Value for argument "fieldPath" is not a valid field path. Paths can't be empty and must not contain "*~/[]".

Here are the tested codes:
Doesn't work -->

db.collectionGroup('mySubCollectionName').where(firebase.firestore.FieldPath.documentId(), '==', myDocumentId)

Works -->

db.collection('myRootCollectionName').where(firebase.firestore.FieldPath.documentId(), '==', myDocumentId)

Does anyone knows how I can trick that? Or if it could be fixed in the next release? Or even if there's a good reason why it is not possible?

Thanks

@google-oss-bot
Copy link

I found a few problems with this issue:

  • I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
  • This issue does not seem to follow the issue template. Make sure you provide all the required information.

@hiranya911
Copy link
Contributor

Ideally this should be reported at https://github.com/googleapis/nodejs-firestore

Assigning to @schmidt-sebastian to review and triage as necessary.

@schmidt-sebastian
Copy link
Contributor

@Cdik Thanks for reporting this! From the error message, it appears that you are passing a single string value (oOuEOw2BFeqypiJmEDuj) which we treat as a collection reference for a root collection.

Collection group queries with document filters require full document paths and must include the collection for the document you are querying. Notice that for db.collection('myRootCollectionName'), you can pass a single document ID since all document paths will just be "myRootCollectionName/{someId}". For collection group queries, however, we don't know where in the hierarchy your document lives. Hence, you will need to pass the full document path (relative to the database root).

Can you double-check that "myDocumentId" points to a document? Alternatively, you can also pass a DocumentReference instead of a string.

See this test case for an example: https://github.com/googleapis/nodejs-firestore/blob/fc6f8392fd07ee93f5321be6808a78f7cdde3b2b/dev/system-test/firestore.ts#L1398

@jlara310
Copy link

jlara310 commented Oct 22, 2019

I have a similar question. I have documents organized into companies/{company}/users/{userid}, and I want to find take a userid and find out what companies employ that user:

db.collectionGroup('users').where(Firestore.FieldPath.documentId(), '==', 'bar')
                                           .limit(1)
                                           .get()
                                           .then(function (querySnapshot) {
                                               querySnapshot.forEach(function (doc) {
                                               console.log(doc.id, ' => ', doc.data());
                                               });
                                           }).catch(function(error) {
                                               console.log("Error getting documents: ", error);
                                          });

Error getting documents: { Error: 3 INVALID_ARGUMENT: Resource name "projects/my-db/databases/(default)/documents/bar" lacks "/" at index 51.

I see that I can add userid as a field and it will work:

db.collectionGroup('users').where('userid', '==', 'bar')

Is there a way to do this using the documentId() sentinel? I guess I thought that it only matched against the very last part of the document name but it seems it doesn't?

@Cdik
Copy link
Author

Cdik commented Oct 22, 2019

@schmidt-sebastian

Better late than never 😄 but it looks like @jlara310 has the same issue.

I get what you mean by not knowing the document path but it will make no sense to pass the full path as it is what I am looking for.

db.collection('myRootCollectionName').where(`/myRootCollectionName/{someId_that_I_do_not_know}/{mySubCollectionName}/${firebase.firestore.FieldPath.documentId()}`, '==', myDocumentId)

where someId_that_I_do_not_know is the doc ID I'm actually looking for. 🤔 Is that clear? I hope...

And yes, "myDocumentId" points to a document. And again, how could I pass a DocumentReference if, it is what I'm looking for.

Thanks in advance. Hope it helps. Cheers

@schmidt-sebastian
Copy link
Contributor

You should be able to pass a DocumentReference, and the SDK will do the right thing (or attempt to).

@frangulyan
Copy link

@schmidt-sebastian I have exactly the same issue. Using the full path of the document completely defeats the purpose of collection group queries. In my case I use the user ID for document IDs on different levels.

In my firestore I have documents placed at /rooms/{room ID}/joinedUsers/{userID}/ and I want to find all the rooms where the user with a given userID is joined by doing collection group query like this:

db.collectionGroup("joinedUsers").joinedUsersGroup.where(FieldPath.documentId(), "==", userID)

gives an error "When querying a collection group and ordering by FieldPath.documentId(), the corresponding value must result in a valid document path, but 'XF0c0r4iTYQjJHA0xhUgaZNvffzB' is not because it contains an odd number of segments."

Is this query simply not possible in Firestore and I have to duplicate the user ID in the document that I'm storing?

@dtheodor
Copy link

Yea this is garbage. The point of collection group queries is the ability to do them over several parent documents. Having to specify a single parent document through the full path completely unmakes the purpose, a plain collection query can do this.

@sakai
Copy link

sakai commented Sep 13, 2023

I wonder if doc.ref.path will work... No?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants