0

This is more of a general question, I've been bouncing around multiple posts on StackOverflow and reading what I've found so far in the docs. I haven't found anything super concrete yet to answer the post question which is: what are the benefits of linking documents to one another through the ref type vs. the string type?

As of now, I'm converting all my string "refs" to properly typed "refs". However, since i'm still relatively new to the platform I'm scratching my head wondering if this is even necessary. I assume I'd be just as effective at finding related docs with the string as with a reference.


Also, for the sake of future readers as of me posting this, you can set a ref like so:

db.collection(...).add({
 ...
  reference: firebaseFirestore.doc(
    `lesson_translations/${translationID}`
  ),
  // reference is now typed as a 'ref'
})

I had found other posts on stackoverflow accessing it with .doc(...).ref which doesnt seem to be a thing anymore.

1

3 Answers 3

2

It's mostly a matter of personal preference and perceived convenience. There is not really anything a reference type can do that a string and your own code could not also do.

Having a reference type mostly saves you the trouble of building a new reference object in your code, if that's what you would have done with a string document ID anyway. You can also use it in security rules for the same purpose when it comes time to use get() to fetch another document referenced by a field.

Again, it's personal preference. Do whatever is most convenient for you.

Sign up to request clarification or add additional context in comments.

Comments

1

When you call a document reference, you instantly get the document details while if you take a string path and call it, it will take some time to find that data. It is just a matter of a few milliseconds and a few lines of code. Imagine if you had a list of document references to store and you store them as strings, and then had to call all of them looping over it

2 Comments

The question isn't asking about "calling" a document reference. It's asking about the practical difference between storing a string and storing a reference when referencing one document from another.
Yeah but I was talking about how you store it will affect him in the future calling it. In storing, its not much of a difference. Maybe there is a few bytes of difference but it does not matter a lot
0

I know of one tangible benefit, but it's only relevant if you are using a library for data binding that supports loading nested documents to n depth. I personally am using vuefire, but I would assume something similar exists for the react ecosystem.

It allows you to e.g. bind a collection or document to a variable, and automatically sync all the data of referenced documents.

Example

You sync a collection('users') with a depth of two, and each user document contains an array of favourites:

{ 
  name:'John',
  favourites: [ref1, ref2, ref3]
}

The library would immediately download the references and replace the original ref files with the data:

{ 
  name:'John',
  favourites: [{title:'foo'}, {title:'bar', {title: 'baz'}]
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.