firestore get id of document code example

Example 1: firestore get all documents in collection

// retrieve a collection
db.collection('documents')
  .get()
  .then(querySnapshot => {
    const documents = querySnapshot.docs.map(doc => doc.data())
    // do something with documents
  })

// retrieve a document
db.collection('documents')
  .doc(documentId)
  .get()
  .then(snapshot => {
    const document = snapshot.data()
    // do something with document
  })

Example 2: firestore get id of new document

async function addCity(newCity) {
  const { id } = await db.collection("cities").add(newCity)
  console.log("the new city's id:", id)
}

Example 3: get one document based on id in firestore

//retrieve one document and save it to userDetails
const [userDetails, setUserDetails] = useState('')
db.collection('users').doc(id).get()
        .then(snapshot => setUserDetails(snapshot.data()))

Example 4: get doc id firestore

const racesCollection: AngularFirestoreCollection<Race>;
return racesCollection.snapshotChanges().map(actions => {       
  return actions.map(a => {
    const data = a.payload.doc.data() as Race;
    data.id = a.payload.doc.id;
    return data;
  });
});

Example 5: how to get firebase document id angular

.valueChanges({ idField: 'propertyId' });