Class FirestoreDb
A Firestore database. Create instances using the static Create(string, FirestoreClient) and CreateAsync(string, FirestoreClient) methods, or using a FirestoreDbBuilder.
Namespace: Google.Cloud.Firestore
Assembly: Google.Cloud.Firestore.dll
Syntax
public sealed class FirestoreDb
Properties
Client
The underlying client used to make service requests.
Declaration
public FirestoreClient Client { get; }
Property Value
Type | Description |
---|---|
FirestoreClient |
DatabaseId
The database ID associated with this database.The value will be "(default)" for the project-default database.
Declaration
public string DatabaseId { get; }
Property Value
Type | Description |
---|---|
string |
ProjectId
The project ID associated with this database.
Declaration
public string ProjectId { get; }
Property Value
Type | Description |
---|---|
string |
Methods
Collection(string)
Creates a local CollectionReference for the given path, which must include an odd number of slash-separated identifiers. This does not perform any remote operations.
Declaration
public CollectionReference Collection(string path)
Parameters
Type | Name | Description |
---|---|---|
string | path | The collection path, e.g. |
Returns
Type | Description |
---|---|
CollectionReference | A collection reference. |
CollectionGroup(string)
Creates and returns a new Query that includes all documents in the database that are contained in a collection or subcollection with the given collection ID.
Declaration
public Query CollectionGroup(string collectionId)
Parameters
Type | Name | Description |
---|---|---|
string | collectionId | Identifies the collections to query over. Every collection or subcollection with this ID as the last segment of its path will be included. Must not contain a slash. |
Returns
Type | Description |
---|---|
Query | The created Query. |
Create(string, FirestoreClient)
Creates an instance for the specified project, using the specified Google.Cloud.Firestore.V1.FirestoreClient for RPC operations.
Declaration
public static FirestoreDb Create(string projectId = null, FirestoreClient client = null)
Parameters
Type | Name | Description |
---|---|---|
string | projectId | The ID of the Google Cloud project that contains the database. May be null, in which case the project will be automatically detected if possible. |
FirestoreClient | client | The client to use for RPC operations. May be null, in which case a client will be created with default credentials. |
Returns
Type | Description |
---|---|
FirestoreDb | A new instance. |
CreateAsync(string, FirestoreClient)
Asynchronously creates an instance for the specified project, using the specified Google.Cloud.Firestore.V1.FirestoreClient for RPC operations.
Declaration
public static Task<FirestoreDb> CreateAsync(string projectId = null, FirestoreClient client = null)
Parameters
Type | Name | Description |
---|---|---|
string | projectId | The ID of the Google Cloud project that contains the database. May be null, in which case the project will be automatically detected if possible. |
FirestoreClient | client | The client to use for RPC operations. May be null, in which case a client will be created with default credentials. |
Returns
Type | Description |
---|---|
Task<FirestoreDb> | A task representing the asynchronous operation. When complete, the result of the task is the new instance. |
Document(string)
Creates a local DocumentReference for the given path, which must include an even number of slash-separated identifiers. This does not perform any remote operations.
Declaration
public DocumentReference Document(string path)
Parameters
Type | Name | Description |
---|---|---|
string | path | The document path, e.g. |
Returns
Type | Description |
---|---|
DocumentReference | A document reference. |
GetAllSnapshotsAsync(IEnumerable<DocumentReference>, FieldMask, CancellationToken)
Fetches document snapshots from the server, potentially limiting the fields returned.
Declaration
public Task<IList<DocumentSnapshot>> GetAllSnapshotsAsync(IEnumerable<DocumentReference> documents, FieldMask fieldMask, CancellationToken cancellationToken = default)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<DocumentReference> | documents | The document references to fetch. Must not be null, or contain null references. |
FieldMask | fieldMask | The field mask to use to restrict which fields are retrieved. May be null, in which case no field mask is applied, and the complete documents are retrieved. |
CancellationToken | cancellationToken | A cancellation token for the operation. |
Returns
Type | Description |
---|---|
Task<IList<DocumentSnapshot>> | The document snapshots, in the same order as |
Remarks
Any documents which are missing are represented in the returned list by a DocumentSnapshot
with Exists value of false
.
GetAllSnapshotsAsync(IEnumerable<DocumentReference>, CancellationToken)
Fetches document snapshots from the server.
Declaration
public Task<IList<DocumentSnapshot>> GetAllSnapshotsAsync(IEnumerable<DocumentReference> documents, CancellationToken cancellationToken = default)
Parameters
Type | Name | Description |
---|---|---|
IEnumerable<DocumentReference> | documents | The document references to fetch. Must not be null, or contain null references. |
CancellationToken | cancellationToken | A cancellation token for the operation. |
Returns
Type | Description |
---|---|
Task<IList<DocumentSnapshot>> | The document snapshots, in the same order as |
Remarks
Any documents which are missing are represented in the returned list by a DocumentSnapshot
with Exists value of false
.
ListRootCollectionsAsync()
Asynchronously retrieves the root collection IDs from the server.
Declaration
public IAsyncEnumerable<CollectionReference> ListRootCollectionsAsync()
Returns
Type | Description |
---|---|
IAsyncEnumerable<CollectionReference> | All the root collection references, in a lazily-iterated sequence. |
RunTransactionAsync(Func<Transaction, Task>, TransactionOptions, CancellationToken)
Runs a transaction asynchronously, with an asynchronous callback that doesn't return a value. The specified callback is executed for a newly-created transaction. If committing the transaction fails, the whole operation is retried based on MaxAttempts.
Declaration
public Task RunTransactionAsync(Func<Transaction, Task> callback, TransactionOptions options = null, CancellationToken cancellationToken = default)
Parameters
Type | Name | Description |
---|---|---|
Func<Transaction, Task> | callback | The callback to execute. Must not be null. |
TransactionOptions | options | The options for the transaction. May be null, in which case default options will be used. |
CancellationToken | cancellationToken | A cancellation token for the operation. This is exposed to the callback through CancellationToken and applied to all RPCs to begin, rollback or commit the transaction. |
Returns
Type | Description |
---|---|
Task | A task which completes when the transaction has committed. |
RunTransactionAsync<T>(Func<Transaction, Task<T>>, TransactionOptions, CancellationToken)
Runs a transaction asynchronously, with an asynchronous callback that returns a value. The specified callback is executed for a newly-created transaction. If committing the transaction fails, the whole operation is retried based on MaxAttempts.
Declaration
public Task<T> RunTransactionAsync<T>(Func<Transaction, Task<T>> callback, TransactionOptions options = null, CancellationToken cancellationToken = default)
Parameters
Type | Name | Description |
---|---|---|
Func<Transaction, Task<T>> | callback | The callback to execute. Must not be null. |
TransactionOptions | options | The options for the transaction. May be null, in which case default options will be used. |
CancellationToken | cancellationToken | A cancellation token for the operation. This is exposed to the callback through CancellationToken and applied to all RPCs to begin, rollback or commit the transaction. |
Returns
Type | Description |
---|---|
Task<T> | A task which completes when the transaction has committed. The result of the task then contains the result of the callback. |
Type Parameters
Name | Description |
---|---|
T | The result type of the callback. |
StartBatch()
Creates a write batch, which can be used to commit multiple mutations atomically.
Declaration
public WriteBatch StartBatch()
Returns
Type | Description |
---|---|
WriteBatch | A write batch for this database. |
WithWarningLogger(Action<string>)
Returns a new FirestoreDb with the same project, database and client as this one, but the given writer for warning logs.
Declaration
public FirestoreDb WithWarningLogger(Action<string> warningLogger)
Parameters
Type | Name | Description |
---|---|---|
Action<string> | warningLogger | The logger for warnings. May be null. |
Returns
Type | Description |
---|---|
FirestoreDb | A new FirestoreDb based on this one, with the given warning logger. |