require"google/cloud/firestore"firestore=Google::Cloud::Firestore.new# Get a collection referencecities_col=firestore.col"cities"# Get and print all city documentscities_col.getdo|city|puts"#{city.document_id} has #{city[:population]} residents."end
Methods
#add
defadd(data=nil)->DocumentReference
Create a document with random document identifier.
The operation will fail if the document already exists.
Parameter
data (Hash) — The document's fields and values. Optional.
require"google/cloud/firestore"firestore=Google::Cloud::Firestore.new# Get a collection referencecities_col=firestore.col"cities"# Get a document reference without datarandom_ref=cities_col.add# The document ID is randomly generatedrandom_ref.document_id#=> "RANDOMID123XYZ"
Create a document snapshot:
require"google/cloud/firestore"firestore=Google::Cloud::Firestore.new# Get a collection referencecities_col=firestore.col"cities"# Get a document snapshotrandom_ref=cities_col.add({name:"New York City"})# The document ID is randomly generatedrandom_ref.document_id#=> "RANDOMID123XYZ"
#collection_id
defcollection_id()->String
The collection identifier for the collection resource.
Returns
(String) — collection identifier.
#collection_path
defcollection_path()->String
A string representing the path of the collection, relative to the
document root of the database.
document_path (String, nil) — A string representing the path of
the document, relative to the document root of the database. If a
string is not provided, and random document identifier will be
generated. Optional.
require"google/cloud/firestore"firestore=Google::Cloud::Firestore.new# Get a collection referencecities_col=firestore.col"cities"# Get a document referencenyc_ref=cities_col.doc"NYC"# The document ID is what was providednyc_ref.document_id#=> "NYC"
Create a document reference with a random ID:
require"google/cloud/firestore"firestore=Google::Cloud::Firestore.new# Get a collection referencecities_col=firestore.col"cities"# Get a document reference without specifying pathrandom_ref=cities_col.doc# The document ID is randomly generatedrandom_ref.document_id#=> "RANDOMID123XYZ"
document_path (String, nil) — A string representing the path of
the document, relative to the document root of the database. If a
string is not provided, and random document identifier will be
generated. Optional.
require"google/cloud/firestore"firestore=Google::Cloud::Firestore.new# Get a collection referencecities_col=firestore.col"cities"# Get a document referencenyc_ref=cities_col.doc"NYC"# The document ID is what was providednyc_ref.document_id#=> "NYC"
Create a document reference with a random ID:
require"google/cloud/firestore"firestore=Google::Cloud::Firestore.new# Get a collection referencecities_col=firestore.col"cities"# Get a document reference without specifying pathrandom_ref=cities_col.doc# The document ID is randomly generatedrandom_ref.document_id#=> "RANDOMID123XYZ"
Retrieves a list of document references for the 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 DocumentReference#get) will return
a DocumentSnapshot whose exists? method returns false.
Parameters
token (String) (defaults to: nil) — A previously-returned page token representing
part of the larger set of results to view.
max (Integer) (defaults to: nil) — Maximum number of results to return.
read_time (Time) (defaults to: nil) — Reads documents as they were at the given time.
This may not be older than 270 seconds. Optional
The document reference or database the collection reference belongs
to. If the collection is a root collection, it will return the client
object. If the collection is nested under a document, it will return
the document reference object.
require"google/cloud/firestore"firestore=Google::Cloud::Firestore.new# Get a collection referencecities_col=firestore.col"cities"# Get the document's parent collectiondatabase=cities_col.parent
Returns document object for nested collections:
require"google/cloud/firestore"firestore=Google::Cloud::Firestore.new# Get a collection referenceprecincts_ref=firestore.col"cities/NYC/precincts"# Get the document's parent collectionnyc_ref=precincts_ref.parent
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-09 UTC."],[],[],null,["# Cloud Firestore API - Class Google::Cloud::Firestore::CollectionReference (v3.1.0)\n\nVersion latestkeyboard_arrow_down\n\n- [3.1.0 (latest)](/ruby/docs/reference/google-cloud-firestore/latest/Google-Cloud-Firestore-CollectionReference)\n- [3.0.0](/ruby/docs/reference/google-cloud-firestore/3.0.0/Google-Cloud-Firestore-CollectionReference)\n- [2.16.1](/ruby/docs/reference/google-cloud-firestore/2.16.1/Google-Cloud-Firestore-CollectionReference)\n- [2.15.1](/ruby/docs/reference/google-cloud-firestore/2.15.1/Google-Cloud-Firestore-CollectionReference)\n- [2.14.0](/ruby/docs/reference/google-cloud-firestore/2.14.0/Google-Cloud-Firestore-CollectionReference)\n- [2.13.1](/ruby/docs/reference/google-cloud-firestore/2.13.1/Google-Cloud-Firestore-CollectionReference)\n- [2.12.0](/ruby/docs/reference/google-cloud-firestore/2.12.0/Google-Cloud-Firestore-CollectionReference)\n- [2.11.0](/ruby/docs/reference/google-cloud-firestore/2.11.0/Google-Cloud-Firestore-CollectionReference)\n- [2.10.1](/ruby/docs/reference/google-cloud-firestore/2.10.1/Google-Cloud-Firestore-CollectionReference)\n- [2.9.1](/ruby/docs/reference/google-cloud-firestore/2.9.1/Google-Cloud-Firestore-CollectionReference)\n- [2.8.0](/ruby/docs/reference/google-cloud-firestore/2.8.0/Google-Cloud-Firestore-CollectionReference)\n- [2.7.2](/ruby/docs/reference/google-cloud-firestore/2.7.2/Google-Cloud-Firestore-CollectionReference)\n- [2.6.6](/ruby/docs/reference/google-cloud-firestore/2.6.6/Google-Cloud-Firestore-CollectionReference) \nReference documentation and code samples for the Cloud Firestore API class Google::Cloud::Firestore::CollectionReference.\n\nCollectionReference\n-------------------\n\nA collection reference object is used for adding documents, getting\ndocument references, and querying for documents (See [Query](/ruby/docs/reference/google-cloud-firestore/latest/Google-Cloud-Firestore-Query \"Google::Cloud::Firestore::Query (class)\")). \n\nInherits\n--------\n\n- [Google::Cloud::Firestore::Query](./Google-Cloud-Firestore-Query)\n\nExample\n-------\n\n```ruby\nrequire \"google/cloud/firestore\"\n\nfirestore = Google::Cloud::Firestore.new\n\n# Get a collection reference\ncities_col = firestore.col \"cities\"\n\n# Get and print all city documents\ncities_col.get do |city|\n puts \"#{city.document_id} has #{city[:population]} residents.\"\nend\n```\n\nMethods\n-------\n\n### #add\n\n def add(data = nil) -\u003e DocumentReference\n\nCreate a document with random document identifier.\n\n\n\u003cbr /\u003e\n\nThe operation will fail if the document already exists. \n**Parameter**\n\n- **data** (Hash) --- The document's fields and values. Optional. \n**Returns**\n\n- ([DocumentReference](./Google-Cloud-Firestore-DocumentReference)) --- A created document.\n**Examples**\n\nCreate a document with a random ID: \n\n```ruby\nrequire \"google/cloud/firestore\"\n\nfirestore = Google::Cloud::Firestore.new\n\n# Get a collection reference\ncities_col = firestore.col \"cities\"\n\n# Get a document reference without data\nrandom_ref = cities_col.add\n\n# The document ID is randomly generated\nrandom_ref.document_id #=\u003e \"RANDOMID123XYZ\"\n```\n\nCreate a document snapshot: \n\n```ruby\nrequire \"google/cloud/firestore\"\n\nfirestore = Google::Cloud::Firestore.new\n\n# Get a collection reference\ncities_col = firestore.col \"cities\"\n\n# Get a document snapshot\nrandom_ref = cities_col.add({ name: \"New York City\" })\n\n# The document ID is randomly generated\nrandom_ref.document_id #=\u003e \"RANDOMID123XYZ\"\n```\n\n### #collection_id\n\n def collection_id() -\u003e String\n\nThe collection identifier for the collection resource. \n**Returns**\n\n- (String) --- collection identifier.\n\n### #collection_path\n\n def collection_path() -\u003e String\n\nA string representing the path of the collection, relative to the\ndocument root of the database. \n**Returns**\n\n- (String) --- collection path.\n\n### #doc\n\n def doc(document_path = nil) -\u003e DocumentReference\n\n**Aliases**\n\n- [#document](./Google-Cloud-Firestore-CollectionReference#Google__Cloud__Firestore__CollectionReference_document_instance_) \nRetrieves a document reference. \n**Parameter**\n\n- **document_path** (String, nil) --- A string representing the path of the document, relative to the document root of the database. If a string is not provided, and random document identifier will be generated. Optional. \n**Returns**\n\n- ([DocumentReference](./Google-Cloud-Firestore-DocumentReference)) --- A document.\n**Examples** \n\n```ruby\nrequire \"google/cloud/firestore\"\n\nfirestore = Google::Cloud::Firestore.new\n\n# Get a collection reference\ncities_col = firestore.col \"cities\"\n\n# Get a document reference\nnyc_ref = cities_col.doc \"NYC\"\n\n# The document ID is what was provided\nnyc_ref.document_id #=\u003e \"NYC\"\n```\n\nCreate a document reference with a random ID: \n\n```ruby\nrequire \"google/cloud/firestore\"\n\nfirestore = Google::Cloud::Firestore.new\n\n# Get a collection reference\ncities_col = firestore.col \"cities\"\n\n# Get a document reference without specifying path\nrandom_ref = cities_col.doc\n\n# The document ID is randomly generated\nrandom_ref.document_id #=\u003e \"RANDOMID123XYZ\"\n```\n\n### #document\n\n def document(document_path = nil) -\u003e DocumentReference\n\n**Alias Of** : [#doc](./Google-Cloud-Firestore-CollectionReference#Google__Cloud__Firestore__CollectionReference_doc_instance_) \nRetrieves a document reference. \n**Parameter**\n\n- **document_path** (String, nil) --- A string representing the path of the document, relative to the document root of the database. If a string is not provided, and random document identifier will be generated. Optional. \n**Returns**\n\n- ([DocumentReference](./Google-Cloud-Firestore-DocumentReference)) --- A document.\n**Examples** \n\n```ruby\nrequire \"google/cloud/firestore\"\n\nfirestore = Google::Cloud::Firestore.new\n\n# Get a collection reference\ncities_col = firestore.col \"cities\"\n\n# Get a document reference\nnyc_ref = cities_col.doc \"NYC\"\n\n# The document ID is what was provided\nnyc_ref.document_id #=\u003e \"NYC\"\n```\n\nCreate a document reference with a random ID: \n\n```ruby\nrequire \"google/cloud/firestore\"\n\nfirestore = Google::Cloud::Firestore.new\n\n# Get a collection reference\ncities_col = firestore.col \"cities\"\n\n# Get a document reference without specifying path\nrandom_ref = cities_col.doc\n\n# The document ID is randomly generated\nrandom_ref.document_id #=\u003e \"RANDOMID123XYZ\"\n```\n\n### #list_documents\n\n def list_documents(token: nil, max: nil, read_time: nil) -\u003e Array\u003cDocumentReference\u003e\n\nRetrieves a list of document references for the documents in this\ncollection.\n\n\n\u003cbr /\u003e\n\nThe document references returned may include references to \"missing\ndocuments\", i.e. document locations that have no document present but\nwhich contain subcollections with documents. Attempting to read such a\ndocument reference (e.g. via [DocumentReference#get](/ruby/docs/reference/google-cloud-firestore/latest/Google-Cloud-Firestore-DocumentReference#Google__Cloud__Firestore__DocumentReference_get_instance_ \"Google::Cloud::Firestore::DocumentReference#get (method)\")) will return\na [DocumentSnapshot](/ruby/docs/reference/google-cloud-firestore/latest/Google-Cloud-Firestore-DocumentSnapshot \"Google::Cloud::Firestore::DocumentSnapshot (class)\") whose `exists?` method returns false. \n**Parameters**\n\n- **token** (String) *(defaults to: nil)* --- A previously-returned page token representing part of the larger set of results to view.\n- **max** (Integer) *(defaults to: nil)* --- Maximum number of results to return.\n- **read_time** (Time) *(defaults to: nil)* --- Reads documents as they were at the given time. This may not be older than 270 seconds. Optional \n**Returns**\n\n- (Array\\\u003c[DocumentReference](./Google-Cloud-Firestore-DocumentReference)\\\u003e) --- An array of document references.\n**Examples** \n\n```ruby\nrequire \"google/cloud/firestore\"\n\nfirestore = Google::Cloud::Firestore.new\n\ncol = firestore.col \"cities\"\n\ncol.list_documents.each do |doc_ref|\n puts doc_ref.document_id\nend\n```\n\nList documents with read time \n\n```ruby\nrequire \"google/cloud/firestore\"\n\nfirestore = Google::Cloud::Firestore.new\n\nread_time = Time.now\n\ncol = firestore.col \"cities\"\n\ncol.list_documents(read_time: read_time).each do |doc_ref|\n puts doc_ref.document_id\nend\n```\n\n### #parent\n\n def parent() -\u003e Client, DocumentReference\n\nThe document reference or database the collection reference belongs\nto. If the collection is a root collection, it will return the client\nobject. If the collection is nested under a document, it will return\nthe document reference object. \n**Returns**\n\n- ([Client](./Google-Cloud-Firestore-Client), [DocumentReference](./Google-Cloud-Firestore-DocumentReference)) --- parent object.\n**Examples**\n\nReturns client object for root collections: \n\n```ruby\nrequire \"google/cloud/firestore\"\n\nfirestore = Google::Cloud::Firestore.new\n\n# Get a collection reference\ncities_col = firestore.col \"cities\"\n\n# Get the document's parent collection\ndatabase = cities_col.parent\n```\n\nReturns document object for nested collections: \n\n```ruby\nrequire \"google/cloud/firestore\"\n\nfirestore = Google::Cloud::Firestore.new\n\n# Get a collection reference\nprecincts_ref = firestore.col \"cities/NYC/precincts\"\n\n# Get the document's parent collection\nnyc_ref = precincts_ref.parent\n```"]]