1

Im looking to have a document, which contains a collection of references to other documents. Like a Project that has a collection of Users, but the entries are Reference types to users from the top level Users collection.

I can't see a way of adding a Reference type to a Collection, It looks like you must create a Document first and then have a field in that doc that is the reference to the user doc - but that is just adding extra work.

When getting the collection data, you will have to first get the Document in the collection, and then using the field for the reference get the User Document.

It would be far simpler to have a nested array of values.

What is the best way to store a collection of references ?

Matt Bryson
  • 2,286
  • 2
  • 22
  • 42

1 Answers1

1

A Reference is a field type, and fields must be in documents. You indeed cannot add References directly to a collection. But from your description I don't think this requires extra dummy documents:

Im looking to have a document, which contains a collection of references to other documents. Like a Project that has a collection of Users, but the entries are Reference types to users from the top level Users collection.

This sounds like you have two top-levels collections: Projects and Users. A document under Projects then has a nested map of user references.

If I misunderstood your model, then indeed: you will need to create a document to store your references to users. This is inherent to the Firestore data model, and can't be changed.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • Thanks for the clarification. A nested array suits my requirements, I suppose i'm now questioning the point of Reference Type - is it just for data integrity? In terms of integrating it into my app (Angular / AngularFire) . I have to constantly convert a Ref to and id, and then the id back to the Ref - i'm considering just using string ids instead of ref types, as the UI will manage the integrity to an extent.. What are the other benefits of Ref types? – Matt Bryson Dec 20 '17 at 16:58
  • Sorry, I haven't dug into the Reference type enough yet to have an answer for that myself. But this question seems to cover it pretty well: [What is firestore Reference data type good for?](https://stackoverflow.com/questions/46568850/what-is-firestore-reference-data-type-good-for) – Frank van Puffelen Dec 20 '17 at 17:47