Querying by a field with type 'reference' in Firestore

前端 未结 1 1917
清歌不尽
清歌不尽 2020-12-02 23:26

I have a collection called \'categories\' containing a single document with ID: 5gF5FqRPvdroRF8isOwd.

I have another collection called \'tickets\'. Each ticket has a

相关标签:
1条回答
  • 2020-12-03 00:06

    As you will read here in the doc, the Reference Data Type is used to store DocumentReferences.

    If you want to use it in a query, you cannot use a simple string, neither the UID of the document (i.e. '5gF5FqRPvdroRF8isOwd'), nor the string value that is stored in the field (i.e. '/categories/5gF5FqRPvdroRF8isOwd').

    You have to build a DocumentReference and use it in your query, as follows:

    const categoryDocRef = firebase.firestore()
       .collection('categories')
       .doc('5gF5FqRPvdroRF8isOwd');
    
    const files = await firebase
      .firestore()
      .collection('tickets')
      .where('category', '==', categoryDocRef)
      .get();
    
    0 讨论(0)
提交回复
热议问题