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 on how to create and apply a state store configuration.

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: <NAME>
  5. spec:
  6. type: state.mongodb
  7. version: v1
  8. metadata:
  9. - name: server
  10. value: <REPLACE-WITH-SERVER> # Required unless "host" field is set . Example: "server.example.com"
  11. - name: host
  12. value: <REPLACE-WITH-HOST> # Required unless "server" field is set . Example: "mongo-mongodb.default.svc.cluster.local:27017"
  13. - name: username
  14. value: <REPLACE-WITH-USERNAME> # Optional. Example: "admin"
  15. - name: password
  16. value: <REPLACE-WITH-PASSWORD> # Optional.
  17. - name: databaseName
  18. value: <REPLACE-WITH-DATABASE-NAME> # Optional. default: "daprStore"
  19. - name: collectionName
  20. value: <REPLACE-WITH-COLLECTION-NAME> # Optional. default: "daprCollection"
  21. - name: writeConcern
  22. value: <REPLACE-WITH-WRITE-CONCERN> # Optional.
  23. - name: readConcern
  24. value: <REPLACE-WITH-READ-CONCERN> # Optional.
  25. - name: operationTimeout
  26. value: <REPLACE-WITH-OPERATION-TIMEOUT> # Optional. default: "5s"
  27. - name: params
  28. value: <REPLACE-WITH-ADDITIONAL-PARAMETERS> # Optional. Example: "?authSource=daprStore&ssl=true"

Warning

The above example uses secrets as plain strings. It is recommended to use a secret store for the secrets as described here.

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:

  1. - name: actorStateStore
  2. value: "true"

Spec metadata fields

FieldRequiredDetailsExample
serverY1The server to connect to, when using DNS SRV record“server.example.com”
hostY1The host to connect to“mongo-mongodb.default.svc.cluster.local:27017”
usernameNThe username of the user to connect with (applicable in conjunction with host)“admin”
passwordNThe password of the user (applicable in conjunction with host)“password”
databaseNameNThe name of the database to use. Defaults to “daprStore”“daprStore”
collectionNameNThe name of the collection to use. Defaults to “daprCollection”“daprCollection”
writeConcernNThe write concern to use“majority”
readConcernNThe read concern to use“majority”, “local”,“available”, “linearizable”, “snapshot”
operationTimeoutNThe timeout for the operation. Defaults to “5s”“5s”
paramsN2Additional parameters to use“?authSource=daprStore&ssl=true”

[1] The server and host 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

You can run a single MongoDB instance locally using Docker:

  1. 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.

In order to use the MongoDB state store for transactions and as an actor state store, you need to run MongoDB as a Replica Set. Refer to the official documentation for how to create a 3-node Replica Set using Docker.

You can conveniently install MongoDB on Kubernetes using the Helm chart packaged by Bitnami. 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). This installs MongoDB into the default namespace. To interact with MongoDB, find the service with: kubectl get svc mongo-mongodb. For example, if installing using the Helm defaults above, the MongoDB host address would be: mongo-mongodb.default.svc.cluster.local:27017 Follow the on-screen instructions to get the root password for MongoDB. The username is typically admin by default.

TTLs and cleanups

This state store supports Time-To-Live (TTL) 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”.

Last modified March 21, 2024: Merge pull request #4082 from newbe36524/v1.13 (f4b0938)