5.2 KiB
type | title | linkTitle | description | aliases | |
---|---|---|---|---|---|
docs | MongoDB | MongoDB | Detailed information on the MongoDB state store component |
|
Component format
To setup MongoDB state store create a component of type state.mongodb
. See [this guide]({{< ref "howto-get-save-state.md#step-1-setup-a-state-store" >}}) on how to create and apply a state store configuration.
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: <NAME>
spec:
type: state.mongodb
version: v1
metadata:
- name: server
value: <REPLACE-WITH-SERVER> # Required unless "host" field is set . Example: "server.example.com"
- name: host
value: <REPLACE-WITH-HOST> # Required unless "server" field is set . Example: "mongo-mongodb.default.svc.cluster.local:27017"
- name: username
value: <REPLACE-WITH-USERNAME> # Optional. Example: "admin"
- name: password
value: <REPLACE-WITH-PASSWORD> # Optional.
- name: databaseName
value: <REPLACE-WITH-DATABASE-NAME> # Optional. default: "daprStore"
- name: collectionName
value: <REPLACE-WITH-COLLECTION-NAME> # Optional. default: "daprCollection"
- name: writeConcern
value: <REPLACE-WITH-WRITE-CONCERN> # Optional.
- name: readConcern
value: <REPLACE-WITH-READ-CONCERN> # Optional.
- name: operationTimeout
value: <REPLACE-WITH-OPERATION-TIMEOUT> # Optional. default: "5s"
- name: params
value: <REPLACE-WITH-ADDITIONAL-PARAMETERS> # Optional. Example: "?authSource=daprStore&ssl=true"
{{% alert title="Warning" color="warning" %}} The above example uses secrets as plain strings. It is recommended to use a secret store for the secrets as described [here]({{< ref component-secrets.md >}}). {{% /alert %}}
Actor state store and transactions support
When using as an actor state store or to leverage transactions, MongoDB must be running in a Replica Set.
If you wish to use MongoDB as an actor store, add this metadata option to your Component YAML:
- name: actorStateStore
value: "true"
Spec metadata fields
Field | Required | Details | Example |
---|---|---|---|
server | Y1 | The server to connect to, when using DNS SRV record | "server.example.com" |
host | Y1 | The host to connect to | "mongo-mongodb.default.svc.cluster.local:27017" |
username | N | The username of the user to connect with (applicable in conjunction with host ) |
"admin" |
password | N | The password of the user (applicable in conjunction with host ) |
"password" |
databaseName | N | The name of the database to use. Defaults to "daprStore" |
"daprStore" |
collectionName | N | The name of the collection to use. Defaults to "daprCollection" |
"daprCollection" |
writeConcern | N | The write concern to use | "majority" |
readConcern | N | The read concern to use | "majority" , "local" ,"available" , "linearizable" , "snapshot" |
operationTimeout | N | The timeout for the operation. Defaults to "5s" |
"5s" |
params | N2 | Additional parameters to use | "?authSource=daprStore&ssl=true" |
[1] The
server
andhost
fields are mutually exclusive. If neither or both are set, Dapr returns an error.
[2] The
params
field accepts a query string that specifies connection specific options as<name>=<value>
pairs, separated by&
and prefixed with?
. e.g. to use "daprStore" db as authentication database and enabling SSL/TLS in connection, specify params as?authSource=daprStore&ssl=true
. See the mongodb manual for the list of available options and their use cases.
Setup MongoDB
{{< tabs "Self-Hosted" "Kubernetes" >}}
{{% codetab %}} You can run MongoDB locally using Docker:
docker run --name some-mongo -d mongo
You can then interact with the server at localhost:27017
.
If you do not specify a databaseName
value in your component definition, make sure to create a database named daprStore
.
{{% /codetab %}}
{{% codetab %}} You can conveniently install MongoDB on Kubernetes using the Helm chart packaged by Bitnami. Please refer to the documentation for the Helm chart for deploying MongoDB, both as a standalone server, and with a Replica Set (required for using transactions and actors). {{% /codetab %}}
{{< /tabs >}}
TTLs and cleanups
This state store supports [Time-To-Live (TTL)]({{< ref state-store-ttl.md >}}) for records stored with Dapr. When storing data using Dapr, you can set the ttlInSeconds
metadata property to indicate when the data should be considered "expired".
Related links
- [Basic schema for a Dapr component]({{< ref component-schema >}})
- Read [this guide]({{< ref "howto-get-save-state.md#step-2-save-and-retrieve-a-single-state" >}}) for instructions on configuring state store components
- [State management building block]({{< ref state-management >}})