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. namespace: <NAMESPACE>
  6. spec:
  7. type: state.mongodb
  8. version: v1
  9. metadata:
  10. - name: host
  11. value: <REPLACE-WITH-HOST> # Required. Example: "mongo-mongodb.default.svc.cluster.local:27017"
  12. - name: username
  13. value: <REPLACE-WITH-USERNAME> # Optional. Example: "admin"
  14. - name: password
  15. value: <REPLACE-WITH-PASSWORD> # Optional.
  16. - name: databaseName
  17. value: <REPLACE-WITH-DATABASE-NAME> # Optional. default: "daprStore"
  18. - name: collectionName
  19. value: <REPLACE-WITH-COLLECTION-NAME> # Optional. default: "daprCollection"
  20. - name: writeconcern
  21. value: <REPLACE-WITH-WRITE-CONCERN> # Optional.
  22. - name: readconcern
  23. value: <REPLACE-WITH-READ-CONCERN> # Optional.
  24. - name: operationTimeout
  25. value: <REPLACE-WITH-OPERATION-TIMEOUT> # Optional. default: "5s"

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
hostYThe host to connect to“mongo-mongodb.default.svc.cluster.local:27017”
usernameNThe username of the user to connect with“admin”
passwordNThe password of the user“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. Defautls to “5s”“5s”

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.

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.

Last modified May 26, 2021: Update to point to 1.2 (#1518) (c690379)