Finding all docs with specific reference in Cloud Firestore

前端 未结 1 1203
渐次进展
渐次进展 2020-12-16 03:24

On Cloud Firestore I have documents with reference to another document this way:

In my example, document Collection A/WJQ9yx67RrqHWQoEp0e2 is r

相关标签:
1条回答
  • 2020-12-16 03:30

    You're right, there's unfortunately no example of actually making use of a Reference data type in the documentation, in fact the only a mention of it is in the Supported Data Types section.

    Ultimately though, a Reference can be used just like any other data type available in Firestore, so can be used to filter & sort data too.

    To achieve what you're after, you would need to construct a Reference that points to the document in Collection B and then use a where clause to filter data on the reference value of Collection A. For example in JavaScript:

    // Create a reference to the specific document you want to search with
    var reference = db.collection("Collection B").doc("rFOEwdw5go4dbitOCXyC");
    
    // Construct a query that filters documents matching the reference
    var query = db.collection("Collection A").where("reference", "==", reference);
    

    Looking at the source for isEqual() in the Firebase JavaScript SDK, comparing of a Reference (extends Query) is performed by simply checking that the paths match:

      isEqual(other: Query): boolean {
        // [...]
        const sameRepo = this.repo === other.repo;
        const samePath = this.path.equals(other.path);
        const sameQueryIdentifier =
          this.queryIdentifier() === other.queryIdentifier();
    
        return sameRepo && samePath && sameQueryIdentifier;
      }
    

    This would seem to be much like calling toString() on both and comparing the string values.

    I produced a similar example yesterday which lead me to test the possible uses of storing a Reference, so that may also apply here.

    0 讨论(0)
提交回复
热议问题