0

I'm trying to use Cloud Firestore to create my app and I have a problem. I have 2 docs that contains reference the same other doc. Which the best way to create this?

Ex:

doc1 = {name: 'name 1', doc3: doc3}

doc2 = {descricao: 'desc 1', doc3: doc3} 

doc3 = {.....}

Anybody help me?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
famadori
  • 83
  • 2
  • 9
  • Do you need to retrieve doc1 and doc2 as their both doc3 property has the same value as `doc3:"doc3"` ??? – Cappittall Feb 17 '18 at 17:14
  • Yes. It is the same object. But i dont know if is the correct way. Or i create the document 3 in other collection docs3 for example and put only idDoc3 in doc2 and doc1? – famadori Feb 17 '18 at 17:30
  • As far as I know, you can not assign a variable to document value something like doc3 value. You must set a known object or any other supported value( String, Number, Object, Array etc.) And if you have assigned a value. then you may retrieve this doc1 and doc2 with this reference code https://stackoverflow.com/questions/48842640/android-firestore-search-collection-where-document-is-unique-id – Cappittall Feb 17 '18 at 17:38
  • Perfect, this way is best for me too. But, this is the first time that i working with cloud firestore. I don´t now what is the best to work with it. Thanks – famadori Feb 17 '18 at 17:48
  • If you try to explain more detail about your aim, then I may suggest u something more. – Cappittall Feb 17 '18 at 19:33

2 Answers2

1

There is a Reference data type which allows you to reference other documents, directly. See the documentation on Supported Data Types

Jason Berryman
  • 4,760
  • 1
  • 26
  • 42
0

You can do any of these:

  1. If you know which collection the other document will always be in, store the id of it, and later use that to build the path to the document to fetch it.
  2. If you don't know which collection the document will be in, either store the full path to it, or store a reference type object that can also be used to fetch the document later.
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441