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.