Reputation: 3955
The firestore docs seem confusing to me. Say I have a set of posts:
posts: [{ name: 'Foo' }, { name: 'Bar' }]
And I want to get a post with the name Foo
.
Following this guide: https://firebase.google.com/docs/firestore/query-data/get-data
It begins with this line:
var docRef = db.collection("cities").doc("SF");
I dont 'understand what .doc("SF")
means. Is SF
an ID? What if my ID was auto-generated? I want to fetch data using this method:
docRef.get().then(function(doc) {
if (doc.exists) {
...
}
}
But confused about the documentation. What's the best way to get()
with my data?
Upvotes: 0
Views: 779
Reputation: 182000
First, make sure you understand the Cloud Firestore data model. It's not the same as Firebase Realtime Database (formerly known as just Firebase).
If the posts
from your example is a collection with two documents, you can use a query to find a document by name
:
var query = db.collection("posts").where("name", "==", "Foo");
var querySnapshot = await query.get();
Now querySnapshot
will be an array of documents that matched your query.
You need an index for any kind of query, but this one is simple enough that it will be automatically created for you.
Upvotes: 2