15

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.

7 Answers 7

35

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");
Sign up to request clarification or add additional context in comments.

7 Comments

This isn't true. You can pass a path to FirebaseFirestore.document().
The path can contain any number of collections and subcollections.
@DougStevenson Right, but you cannot pass a path to the collection method, only to the document, right?
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.
Just changed the rootRef to db, not to be confused.
|
4

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)

Comments

2

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.

3 Comments

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
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().
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();.
0

you just need to define the path :

`users/${type}/${user.uid}/`

firestore
  .collection(`users/${type}/${user.uid}/`)
  .add(documentData)
  .then(function () {
    message = { message: "document added", type: ErrorType.success };
  })
  .catch(function (error) {
    message = { message: error, type: ErrorType.error };
  });

Comments

0

For Firestore web version 9 (after August 25, 2021) you can use doc() to generate a reference with an id:

import { collection, doc, setDoc, getFirestore } from "firebase/firestore"; 

// Add a new document with a generated id
const newRef = doc(collection(getFirestore(), "user"));

const data = {
  id: newRef.id,
  firstName: 'John',
  lastName: 'Doe'
}

await setDoc(newRef, data);

Comments

0

Use "." instead of "/" for path

FirebaseFirestore db = FirebaseFirestore.getInstance();

db.collection(FBConstant.Product.Head)
.whereEqualTo("rating.$id", true)

Comments

0

In android (Kotlin) you can achieve this as below :

val firestore = FirebaseFirestore.getInstance()
val docRef = firestore.document(documentPath) as DocumentReference

My Collection Name is : Collection 1 So In mycase, documentPath is having value as : "Collection 1/Document ID"

While fetching the data you can use as

firestore.collection("Collection 1")
.whereEqualTo("Field name",docRef)
.get()
.addOnCompleteListener{...}

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.