Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to query a DocumentReference in Firestore

I have a collection of bar orders, and in each document i have a field with a reference for a barman.

Illustrative image


I'm trying to made a where with the doc reference.

Something like that:

orders = this.database.collection("orders",
    (ref) => ref.where("barman.path", "==", "barmen/" + this.auth.auth.currentUser.uid),
);
return orders.valueChanges();

barman.path because when i get a doc with a field referece, this is the way to get the reference path.

I already tried to use just barman instead barman.path.

I already tried to made where with full 'path' of docref (firestore.googleapis.com/pro...).

Any ideas?

Put only the id instead of the full reference will dificult other parts of the system.

like image 300
José Camelo de Freitas Avatar asked Dec 02 '17 13:12

José Camelo de Freitas


People also ask

How do I get data from firestore query?

How to get query data using firebase firestore? Simply, use a get() call! query. get().

What is DocumentReference firestore?

A DocumentReference refers to a document location in a Cloud Firestore database and can be used to write, read, or listen to the location. There may or may not exist a document at the referenced location. A DocumentReference can also be used to create a CollectionReference to a subcollection.


1 Answers

In order to query a reference field, you should create a DocumentReference object that matches the location you are trying to query for, and use that. Note that this needs to be a native Firestore reference, as an AngularFirestoreCollection and AngularFirestoreDocument will just show up to Firestore as a "custom object type". You can get this with the ref member of the AngularFirestore types.

I'm not sure where the '.path' part of your query is coming form, as the actual field you are querying for is barman. I've removed it in the example.

In your case, this will look something like:

const queryReference = this.database
    .collection('barmen')
    .doc(this.auth.auth.currentUser.uid).ref;

orders = this.database.collection('orders',
    (ref) => ref.where('barman', '==', queryReference),
);
return orders.valueChanges();

I obviously couldn't test this exactly as there isn't enough code to, for example, show that this.auth.auth.currentUser.uid works in this case. But it should point you in the right direction.

like image 114
robsiemb Avatar answered Oct 18 '22 07:10

robsiemb