score:0

Accepted answer

you must use .doc() when you know the document id and then the query goes as follows:

const docref = firebase.firestore().collection("polja").doc("documentid");

but in case you are looking for documents with a specific field value, then you are looking for .where()

const docref = firebase.firestore().collection("polja").where("id", "==", match.params.id);

this can match multiple documents where the value of that field matches the supplied value. so to control the number or results returned, use a limit clause or paginate your query.

if you use .doc() function, then the variable doc in the response is a documentsnapshot. you can then use doc.data() to get its data.

on the other hand, if you use .where(), it'll return firebasefirestore.querysnapshot<firebasefirestore.documentdata> which contains all the documents matching the condition you had specified in the .where(). to access all the documents in that querysnapshot, you must access the docs property like this: snapshot.docs. now this will be an array of documents. you can use snapshot.size to see how many documents have matched your query.

some practical examples to clarify:

//collection in firestore:
users -> {userid as documentid} -> user data in the document
//in this case you know the userid then you can should use the .doc()
const userdocref = firebase.collection("users").doc("userid");
userdocref.get().then((userdata) => {
  console.log(userdata.data())
  //^ this will contain data in side that document
})

//in this case, you want to find firestore documents based on value of any field present in the document, lets say in this example if user's age is more than 15
const userdocsref = firebase.collection("users").where("age", ">=", 15);
userdocsref.get().then((usersdata) => {
  console.log(usersdata.docs)
  //^ this will log an array of documents which matched the condition of having the value of field age greater than or equal to 15
})

Related Query

More Query from same tag