Reference documentation and code samples for the Cloud Firestore Client class CollectionReference.
Represents a Cloud Firestore Collection.
Collections are implicit namespaces for Firestore Documents. They are created when the first document is inserted, and cease to exist when the last document is removed.
Example:
use Google\Cloud\Firestore\FirestoreClient;
$firestore = new FirestoreClient();
$collection = $firestore->collection('users');
Namespace
Google \ Cloud \ FirestoreMethods
__construct
Parameters | |
---|---|
Name | Description |
connection |
Connection\ConnectionInterface
A Connection to Cloud Firestore. This object is created by FirestoreClient, and should not be instantiated outside of this client. |
valueMapper |
ValueMapper
A Firestore Value Mapper. |
name |
string
The absolute name of the collection. |
name
Get the collection name.
Names are absolute. The result of this call would be of the form
projects/<project-id>/databases/<database-id>/documents/<relative-path>
.
Other methods are available to retrieve different parts of a collection name:
- CollectionReference::id() Returns the last element.
- CollectionReference::path() Returns the path, relative to the database.
Example:
$name = $collection->name();
Returns | |
---|---|
Type | Description |
string |
path
Get the collection path.
Paths identify the location of a collection, relative to the database name.
To retrieve the collection ID (the last element of the path), use CollectionReference::id().
Example:
$path = $collection->path();
Returns | |
---|---|
Type | Description |
string |
id
Get the collection ID.
IDs are the path element which identifies a resource. To retrieve the full path to a resource (the resource name), use CollectionReference::name().
Example:
$id = $collection->id();
Returns | |
---|---|
Type | Description |
string |
document
Get a reference to a document which is a direct child of this collection.
Example:
$newUser = $collection->document('john');
Parameter | |
---|---|
Name | Description |
documentId |
string
The document ID. |
Returns | |
---|---|
Type | Description |
DocumentReference |
newDocument
Get a document reference with a randomly generated document ID.
If you wish to create a document reference with a specified name, use CollectionReference::document().
This method does NOT insert the document until you call DocumentReference::create().
Example:
$newUser = $collection->newDocument();
Returns | |
---|---|
Type | Description |
DocumentReference |
add
Generate a new document reference, and insert it with the given field data.
This method will generate a random document name. If you wish to create a document with a specified name, create a reference with CollectionReference::document(), then call DocumentReference::create() to insert the document.
This method immediately inserts the document. If you wish for lazy creation of a Document instance, refer to CollectionReference::document() or CollectionReference::newDocument().
Example:
$newUser = $collection->add([
'name' => 'Kate'
]);
Parameters | |
---|---|
Name | Description |
fields |
array
An array containing field names paired with their value. Accepts a nested array, or a simple array of field paths. |
options |
array
Configuration Options. |
Returns | |
---|---|
Type | Description |
DocumentReference |
listDocuments
See also:
Parameters | |
---|---|
Name | Description |
options |
array
Configuration options |
↳ pageSize |
int
The maximum number of results to return per request. |
↳ resultLimit |
int
Limit the number of results returned in total. Defaults to |
↳ pageToken |
string
A previously-returned page token used to resume the loading of results from a specific point. |
Returns | |
---|---|
Type | Description |
Google\Cloud\Core\Iterator\ItemIterator<DocumentReference> |
parent
Get the parent document reference for a subcollection, or null if root.
Example:
$parent = $collection->parent();
Returns | |
---|---|
Type | Description |
DocumentReference|null |