Here's a picture of my data:
I'm trying to get that document. This works:
var docRef = db.collection('users').doc('jPDKwHyrFNXNTFI5qgOY');
docRef.get().then(function(doc) {
if (doc.exists) {
console.log("Document data:", doc.data());
} else {
console.log("No such document!");
}
}).catch(function(error) {
console.log("Error getting document:", error);
});
It returns:
I.e., if I know the document's key I can get the document.
This doesn't work:
db.collection('users').where('uid', '==', 'bcmrZDO0X5N6kB38MqhUJZ11OzA3')
.get().then(function(querySnapshot) {
if (querySnapshot.exists) {
console.log(querySnapshot.data);
} else {
console.log("No such document!");
}
})
.catch(function(error) {
console.log("Error getting document: ", error);
});
It just returns No such document!
What's wrong with my query?
There are three ways to retrieve data stored in Cloud Firestore. Any of these methods can be used with documents, collections of documents, or the results of queries: Call a method to get the data once. Set a listener to receive data-change events.
When you call the . add method on a collection, a DocumentReference object is returned. DocumentReference has the id field, so you can get the id after the document was created. // Add a new document with a generated id.
In Cloud Firestore, you can use queries to retrieve individual, specific documents or to retrieve all the documents in a collection that match your query parameters. Your queries can include multiple, chained filters and combine filtering and sorting.
Following Cloud Firestore's NoSQL data model, you store data in documents that contain fields mapping to values. These documents are stored in collections, which are containers for your documents that you can use to organize your data and build queries.
This means you can now query, for example, all documents in a "Projects" collection where the project's status field is not equal to the value "completed" On a similar note, Cloud Firestore also supports not-in queries, where you can query for documents where fields are not in a list of values.
This is not possible in Firestore currently. We should understand why. In Firestore, all queries are served off indexes for performance reasons. Most of these indexes on single columns are automatically created for us, which is why when we get started, we don’t realise it.
The difference in your two requests is that in the first case you are retrieving one document which gives you a DocumentSnapshot
which has the exists
property and the data()
method.
In the second case you do a query, which gives you a QuerySnapshot
that has to be handled differently from a DocumentSnapshot
. Instead of a single document you get a list/collection of documents. You can check if data has been retrieved using the empty
or size
properties, and then go through the results using the forEach
method or going through the docs
array:
db.collection('users').where('uid', '==', 'bcmrZDO0X5N6kB38MqhUJZ11OzA3')
.get().then(function(querySnapshot) {
if (querySnapshot.size > 0) {
// Contents of first document
console.log(querySnapshot.docs[0].data());
} else {
console.log("No such document!");
}
})
.catch(function(error) {
console.log("Error getting document: ", error);
});
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