Class CollectionReference (4.15.1)

A CollectionReference object can be used for adding documents, getting document references, and querying for documents (using the methods inherited from [Query] Query ).

CollectionReference Query

Inheritance

Query <T> > CollectionReference

Package

@google-cloud/firestore

Constructors

(constructor)(firestore, path, converter)

  constructor 
 ( 
 firestore 
 : 
  
 Firestore 
 , 
  
 path 
 : 
  
 ResourcePath 
 , 
  
 converter 
 ?: 
  
 firestore 
 . 
 FirestoreDataConverter<T> 
 ); 
 

Constructs a new instance of the CollectionReference class

Parameters
Name Description
firestore Firestore

The Firestore Database client.

path ResourcePath

The Path of this collection.

converter FirebaseFirestore.FirestoreDataConverter <T>

Properties

id

  get 
  
 id 
 () 
 : 
  
 string 
 ; 
 

The last path element of the referenced collection.

{string} CollectionReference#id

Property Value
Type Description
string
Example

let collectionRef = firestore.collection('col/doc/subcollection'); console.log( ID of the subcollection: ${collectionRef.id} );

parent

  get 
  
 parent 
 () 
 : 
  
 DocumentReference<firestore 
 . 
 DocumentData 
>  
 | 
  
 null 
 ; 
 

A reference to the containing Document if this is a subcollection, else null.

{DocumentReference|null} CollectionReference#parent

Property Value
Type Description
DocumentReference < FirebaseFirestore.DocumentData > | null
Example

let collectionRef = firestore.collection('col/doc/subcollection'); let documentRef = collectionRef.parent; console.log( Parent name: ${documentRef.path} );

path

  get 
  
 path 
 () 
 : 
  
 string 
 ; 
 

A string representing the path of the referenced collection (relative to the root of the database).

{string} CollectionReference#path

Property Value
Type Description
string
Example

let collectionRef = firestore.collection('col/doc/subcollection'); console.log( Path of the subcollection: ${collectionRef.path} );

Methods

add(data)

  add 
 ( 
 data 
 : 
  
 T 
 ) 
 : 
  
 Promise<DocumentReference<T> 
> ; 
 

Add a new document to this collection with the specified data, assigning it a document ID automatically.

Parameter
Name Description
data T

An Object containing the data for the new document.

Returns
Type Description
Promise < DocumentReference <T>>

{Promise.

Example

let collectionRef = firestore.collection('col'); collectionRef.add({foo: 'bar'}).then(documentReference => { console.log( Added document with name: ${documentReference.id} ); });

doc()

  doc 
 () 
 : 
  
 DocumentReference<T> 
 ; 
 
Returns
Type Description
DocumentReference <T>

doc(documentPath)

  doc 
 ( 
 documentPath 
 : 
  
 string 
 ) 
 : 
  
 DocumentReference<T> 
 ; 
 
Parameter
Name Description
documentPath string
Returns
Type Description
DocumentReference <T>

isEqual(other)

  isEqual 
 ( 
 other 
 : 
  
 firestore 
 . 
 CollectionReference<T> 
 ) 
 : 
  
 boolean 
 ; 
 

Returns true if this CollectionReference is equal to the provided value.

Parameter
Name Description
other FirebaseFirestore.CollectionReference <T>

The value to compare against. {boolean} true if this CollectionReference is equal to the provided value.

Returns
Type Description
boolean

listDocuments()

  listDocuments 
 () 
 : 
  
 Promise<Array<DocumentReference<T> 
>> ; 
 

Retrieves the list of documents in this collection.

The document references returned may include references to "missing documents", i.e. document locations that have no document present but which contain subcollections with documents. Attempting to read such a document reference (e.g. via .get() or .onSnapshot() ) will return a DocumentSnapshot whose .exists property is false.

{Promise<DocumentReference[]>} The list of documents in this collection.

Returns
Type Description
Promise < Array < DocumentReference <T>>>
Example

let collectionRef = firestore.collection('col');

return collectionRef.listDocuments().then(documentRefs => { return firestore.getAll(...documentRefs); }).then(documentSnapshots => { for (let documentSnapshot of documentSnapshots) { if (documentSnapshot.exists) { console.log( Found document with data: ${documentSnapshot.id} ); } else { console.log( Found missing document: ${documentSnapshot.id} ); } } });

withConverter(converter)

  withConverter 
 ( 
 converter 
 : 
  
 null 
 ) 
 : 
  
 CollectionReference<firestore 
 . 
 DocumentData 
> ; 
 
Parameter
Name Description
converter null
Returns
Type Description
CollectionReference < FirebaseFirestore.DocumentData >

withConverter(converter)

  withConverter<U> 
 ( 
 converter 
 : 
  
 firestore 
 . 
 FirestoreDataConverter<U> 
 ) 
 : 
  
 CollectionReference<U> 
 ; 
 
Parameter
Name Description
converter FirebaseFirestore.FirestoreDataConverter <U>
Returns
Type Description
CollectionReference <U>
Type Parameter
Name Description
U
Create a Mobile Website
View Site in Mobile | Classic
Share by: