amarjeet kumar singh
amarjeet kumar singh

Reputation: 57

Getting document id after querying the Result?

I wanted to ask one thing is it possible to get the document id of the satisfied conditions like i have applied a condition on the particular collection and the query is valid and returned documents and I wanted to get the document id which comes after the querying

const reports = async() => {
const officeCollection = db.collection('XYZ');
  const officeQuerySnapshot = await officeCollection.where('office','==' ,office).get();
  const officeData = []
  var firstContact= ''
  officeQuerySnapshot.forEach(doc => {
    // var firstContact = doc.data().attachment['First Contact']
    // console.log(firstContact)
    officeData.push(doc.data())

  })
}

Upvotes: 1

Views: 81

Answers (2)

Frank van Puffelen
Frank van Puffelen

Reputation: 600131

To print the document ID, you'll want to use doc.id:

const reports = async() => {
const officeCollection = db.collection('XYZ');
  const officeQuerySnapshot = await officeCollection.where('office','==' ,office).get();
  const officeData = []
  var firstContact= ''
  officeQuerySnapshot.forEach(doc => {
    console.log(doc.id)
    officeData.push(doc.data())
  })
}

Upvotes: 1

vitooh
vitooh

Reputation: 4272

if you will use doc.ref instead of doc.data() you will have reference to will get DocumentReference object. Than you can get property like _path to get document id.

I used something like console.log(doc.ref._path) and I get something like that:

enter image description here

Document ID is in the segment list. API reference you can find here.

Upvotes: 0

Related Questions