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.

If you wish to use MongoDB as an actor store, append the following to the yaml.

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

Spec metadata fields

FieldRequiredDetailsExample
serverYThe server to connect to, when using DNS SRV record“server.example.com”
hostYThe 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”
paramsN**Additional parameters to use“?authSource=daprStore&ssl=true”

[*] The server and host fields are mutually exclusive. If neither or both are set, Dapr will return an error.

[**] 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 MongoDB locally using Docker:

  1. docker run --name some-mongo -d mongo

You can then interact with the server using localhost:27017.

If you do not specify a databaseName value in your component definition, make sure to create a database named daprStore.

The easiest way to install MongoDB on Kubernetes is by using the Helm chart:

  1. helm install mongo stable/mongodb

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 example 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 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 October 12, 2023: Update config.toml (#3826) (0ffc2e7)