Firestore - How create DocumentReference using path String
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
.
![Monu](https://i.stack.imgur.com/bSzxe.jpg?s=256&g=1)
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, 2022Comments
-
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 about 6 yearsThis isn't true. You can pass a path to FirebaseFirestore.document().
-
Doug Stevenson about 6 yearsThe path can contain any number of collections and subcollections.
-
Alex Mamo about 6 years@DougStevenson Right, but you cannot pass a path to the
collection
method, only to the document, right? -
Doug Stevenson about 6 yearsI 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/firebase/…
-
Monu about 6 yearsThanks 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 about 6 years@DougStevenson Edited my answer accordingly. Thanks again!
-
Doug Stevenson about 6 yearsWhen I say
FirebaseFirestore.document()
, I mean the method calleddocument()
on an object of typeFirebaseFirestore
. You can get an object of typeFirebaseFirestore
by callingFirebaseFirestore.getInstance()
. -
Monu about 6 yearsI 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 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 about 6 yearsJust changed the
rootRef
todb
, not to be confused.