Firestore - How create DocumentReference using path String

15,770

Solution 1

Yes, you can achieve this also in Cloud Firestore. So these are your options:

FirebaseFirestore db = FirebaseFirestore.getInstance();

First option:

DocumentReference userRef = db.collection("company/users");

Second option:

DocumentReference userRef = db.document("company/users");

Third option:

DocumentReference userRef = db.collection("company").document("users");

Solution 2

For web/javascript, db.doc() will create a DocumentReference from a string:

let docRef = db.doc(pathString)

e.g. let userRef = db.doc('users/' + userId)

Solution 3

You can use FirebaseFirestore.document() and pass it the path of the document you want. Each document must be located within a collection. If you're looking for a document called documentId in a collection called collectionId, the path string will be collectionId/documentId.

Share:
15,770
Monu
Author by

Monu

GitHub - https://github.com/Parveshdhull YouTube Channel - https://www.youtube.com/right2trick Stackoverflow - https://stackoverflow.com/users/7475429/parvesh-monu Reddit - https://www.reddit.com/user/hrca3 Twitter - https://twitter.com/ParveshMonu LinkedIn - https://www.linkedin.com/in/parvesh-monu-054b0875/

Updated on June 07, 2022

Comments

  • Monu
    Monu about 2 years

    Firebase realtime database allowed to create refrence using

     var reference = db.ref(path);
    

    Is there any method exist in firestore so that I can create document refrence using path String.

    If method exists how can I find path string in android and then how create document reference in node.js using that path.

  • Doug Stevenson
    Doug Stevenson about 6 years
    This isn't true. You can pass a path to FirebaseFirestore.document().
  • Doug Stevenson
    Doug Stevenson about 6 years
    The path can contain any number of collections and subcollections.
  • Alex Mamo
    Alex Mamo about 6 years
    @DougStevenson Right, but you cannot pass a path to the collection method, only to the document, right?
  • Doug Stevenson
    Doug Stevenson about 6 years
    I don't think that's what the OP is asking. But the documentation for collection() suggests that it also can take a path. firebase.google.com/docs/reference/android/com/google/fireba‌​se/…
  • Monu
    Monu about 6 years
    Thanks but I think you are not complete right, I tried in android studio there is no direct method FirebaseFirestore.document() first I have to create object of firestore then can call method.....Please edit your answer so that i can accept it
  • Alex Mamo
    Alex Mamo about 6 years
    @DougStevenson Edited my answer accordingly. Thanks again!
  • Doug Stevenson
    Doug Stevenson about 6 years
    When I say FirebaseFirestore.document(), I mean the method called document() on an object of type FirebaseFirestore. You can get an object of type FirebaseFirestore by calling FirebaseFirestore.getInstance().
  • Monu
    Monu about 6 years
    I think variable name for FirebaseFirestore should be like db or firestore because rootRef is little confusing because it is object of databse not refrence.
  • Alex Mamo
    Alex Mamo about 6 years
    document() is not a static method and in order to be able to call it, you need to create an object of the class first. FirebaseFirestore rootRef = FirebaseFirestore.getInstance();.
  • Alex Mamo
    Alex Mamo about 6 years
    Just changed the rootRef to db, not to be confused.