I have been working on transitioning a small Firebase project to the new, similar Firestore db because of its more queryable nature, but I am having a trouble with a simple query problem.
I am trying to get all documents in a collection, in the case each document is a club and the collection is all of the clubs.
In Firebase, I was able to do something like:
export function fetchClubs() {
const Club = firestore.ref('clubs');
return dispatch => {
Club.on('value', snapshot => {
dispatch({
type: FETCH_CLUBS,
payload: snapshot.val()
})
})
}
}
snapshot
would have all the information i needed inside of it.
I am trying to replicate this with Firestore, but have an only been able to come across a solution that involves making a new array and looping over the results then adding each result to that.
Here is my Firestore attempt using the slightly different Firestore syntax
export function fetchClubsStore() {
const Clubs = firestore.collection('clubs');
return dispatch => {
Clubs.get().then(querySnapshot => {
console.log("clubs snapshot: ", querySnapshot)
dispatch({
type: FETCH_CLUBS,
payload: querySnapshot.val()
})
})
}
}
If you have any ideas, all help will be greatly appreciated! Thanks!
(this is my first stackoverflow post, go easy! :) )
Edit:
Example from Firestore docs of how to get all documents in a collection.
db.collection("cities").where("capital", "==", true)
.get()
.then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
console.log(doc.id, " => ", doc.data());
});
})
.catch(function(error) {
console.log("Error getting documents: ", error);
});
Edit 2:
My new solution to get the documents in a usable array by using .docs and .data() . Still seems inefficient, as I am having to loop over the doucments and extract the usable information one by one.
export function fetchClubsStore() {
const Clubs = firestore.collection('clubs');
var clubs = [];
return dispatch => {
Clubs.get().then(querySnapshot => {
console.log("clubs snapshot: ", querySnapshot.docs)
_.each(querySnapshot.docs, function(data){
clubs.push(data.data());
})
dispatch({
type: FETCH_CLUBS,
payload: clubs
})
})
}
}
If you need a count, just use the collection path and prefix it with counters . As this approach uses a single database and document, it is limited to the Firestore constraint of 1 Update per Second for each counter.
With the in query, you can query a specific field for multiple values (up to 10) in a single query. You do this by passing a list containing all the values you want to search for, and Cloud Firestore will match any document whose field equals one of those values.
By default, Cloud Firestore retrieves all documents that satisfy the query in ascending order by document ID, but you can order and limit the data returned. In addition, you can retrieve all documents in a collection by omitting the where () filter entirely:
Therefore, it is possible to use the Cloud Firestore Node.js Client API to write a Cloud Function that lists the subcollections of a document. Since we will call this Cloud Function from the app we use a Callable Cloud Function ⁴.
We use the data parameter to get docPath, the value of the Firestore document path (slash-separated). This value is passed from the client calling the Cloud Function (see below). We then call the asynchronous listCollections () method on the DocumentReference created by using docPath (i.e. admin.firestore ().doc (docPath) ).
When you set a listener, Cloud Firestore sends your listener an initial snapshot of the data, and then another snapshot each time the document changes. Note: While the code samples cover multiple languages, the text explaining the samples refers to the Web method names.
The difference between Firebase Realtime Database and Firestore is that RTDB is basically just a cloud hosted JSON object. So there is no concept of documents and there is no collections of data.
The value object in a query snapshot that you retrieve from RTDB is just a JSON object. Hence you can access first level nodes by their keys (and also any level nodes by their keys)
If you're coming from RTDB you'll notice the difference that Cloud Firestore has collections and documents. So your query results will inherently be collections of data rather than a JSON object. The documents that are retrieved from a query can not be grabbed as a single big JSON object.
Looping over the query result is the correct way to work with Cloud Firestore.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With