Search code examples
javascriptnode.jsfirebasegoogle-cloud-platformgoogle-cloud-firestore

What is Firebase Firestore 'Reference' data type good for?


I'm just exploring the new Firebase Firestore and it contains a data type called reference. It is not clear to me what this does.

  • Is it like foreign key?
  • Can it be used to point to a collection that is located somewhere else?
  • If reference is an actual reference, can I use it for queries? For example can I have a reference that points directly to the user, instead of storing the userId in a text field? And can I use this user reference for querying?

Solution

  • References are very much like foreign keys.

    The currently released SDKs cannot store references to other projects. Within a project, references can point to any other document in any other collection.

    You can use references in queries like any other value: for filtering, ordering, and for paging (startAt/startAfter).

    Unlike foreign keys in a SQL database, references are not useful for performing joins in a single query. You can use them for dependent lookups (which seem join like), but be careful because each hop will result in another round trip to the server.