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

FR: allow DocumentReference.select similar to Query.select #42

Closed
goldensunliu opened this issue Oct 27, 2017 · 7 comments
Closed

FR: allow DocumentReference.select similar to Query.select #42

goldensunliu opened this issue Oct 27, 2017 · 7 comments
Assignees
Labels
api: firestore Issues related to the googleapis/nodejs-firestore API. 🚨 This issue needs some love. triage me I really want to be triaged.

Comments

@goldensunliu
Copy link

goldensunliu commented Oct 27, 2017

Hey guys,
I have been snooping around the source code and documentation. It seems like the only way to do apply a field mask to the result and returns only the specified subset of fields is through the Query.select method.
https://cloud.google.com/nodejs/docs/reference/firestore/0.8.x/Query#select
https://github.com/googleapis/nodejs-firestore/blob/master/src/reference.js#L1080

Is there any plan or ways to enable the same feature when getting a single document via DocumentReference? The code seems plenty of references to https://cloud.google.com/firestore/docs/reference/rest/v1beta1/DocumentMask which is used to restrict a get or update operation on a document to a subset of its fields, but I didn't have any luck tracking down usage of this in DocumentReference.get

Relevant Code:

// based on the select example at https://cloud.google.com/nodejs/docs/reference/firestore/0.8.x/Query#select
let collectionRef = firestore.collection('col');
let documentRef = collectionRef.doc('doc');

documentRef.set({x:10, y:5}).then(() => {
  return collectionRef.where('x', '>', 5).select('y').get();
}).then((res) => {
  console.log(`y is ${res.docs[0].get('y')}.`);
});
// would like to be able to also do
documentRef.select('y').get().then((snapShot) => {
  console.log(`snapShot only has one field ${snapShot}`);
  // Expected output: {y: 5}
});
@goldensunliu
Copy link
Author

I have also filed this against firebase/firebase-js-sdk#272 since I am not sure where the source of development on the firestore part is housed.

@goldensunliu
Copy link
Author

firebase/firebase-js-sdk#212 answers this

@IchordeDionysos
Copy link
Contributor

@goldensunliu Are you sure that that the issue in the Firebase JS SDK really answers this?
As I understood the answer from @wilhuff, is that this won't appear in the JS CLIENT SDK as you are having offline capabilities there and so on.

I don't see a problem in the ADMIN SDK to add such a method to the DocumentReference. As far as I know, there is no caching in the ADMIN SDK, so it should be no problem to allow a snippet/method like this:

admin.firestore().document("path/to/a/document").select("y").get().then(snap => {
    console.log(`snapShot only has one field ${snap.data()}`);
    // Expected output: {y: 5}
});

Or am I wrong?

@wilhuff
Copy link
Contributor

wilhuff commented Jul 27, 2018

@IchordeDionysos you're right: the comments in firebase/firebase-js-sdk#212 specifically relate to the mobile clients and the complications of interacting with the cache while offline. These concerns do not apply to the nodejs or admin SDKs.

document(..).get() translates into a BatchGetDocumentsRequest RPC, which includes a document mask, so this should be possible to implement.

In the meantime you can work around this by writing the following instead:

const projectId = // ... your project id 
const path = 'path/to/a/document';
db.document(path)
    .parent  // i.e. the collection containing the document
    .where('__name__', '==', `projects/${projectId}/databases/(default)/documents/${path}`)
    .select(...)

Note that the result will be a query snapshot rather than a document snapshot, but it should get you what you want in the short term.

@schmidt-sebastian holler if I got this wrong.

@schmidt-sebastian
Copy link
Contributor

A more concise version would be as follows:

const docRef = db.doc('path/to/a/document');
docRef.parent.where(Firestore.FieldPath.documentId(), '==', docRef).select(...)

@triplef
Copy link

triplef commented Nov 5, 2018

It would be great to have this issue re-opened for consideration.

As mentioned above, the comments in firebase/firebase-js-sdk#212 don’t apply to the Admin SDK. It would be great to have a DocumentReference.select() function implemented there.

@schmidt-sebastian
Copy link
Contributor

We are currently debating on how to best implement this. Please stay tuned.

@JustinBeckwith JustinBeckwith added 🚨 This issue needs some love. triage me I really want to be triaged. labels Nov 5, 2018
@google-cloud-label-sync google-cloud-label-sync bot added the api: firestore Issues related to the googleapis/nodejs-firestore API. label Jan 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: firestore Issues related to the googleapis/nodejs-firestore API. 🚨 This issue needs some love. triage me I really want to be triaged.
Projects
None yet
Development

No branches or pull requests

6 participants