Reputation: 907
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.
Upvotes: 15
Views: 20224
Reputation: 71
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{...}
Upvotes: 0
Reputation: 1344
Use "." instead of "/" for path
FirebaseFirestore db = FirebaseFirestore.getInstance();
db.collection(FBConstant.Product.Head)
.whereEqualTo("rating.$id", true)
Upvotes: 0
Reputation: 719
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);
Upvotes: 0
Reputation: 1271
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 };
});
Upvotes: 0
Reputation: 878
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)
Upvotes: 4
Reputation: 317712
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
.
Upvotes: 2
Reputation: 138969
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");
Upvotes: 35