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: server
  11. value: <REPLACE-WITH-SERVER> # Required unless "host" field is set . Example: "server.example.com"
  12. - name: host
  13. value: <REPLACE-WITH-HOST> # Required unless "server" field is set . Example: "mongo-mongodb.default.svc.cluster.local:27017"
  14. - name: username
  15. value: <REPLACE-WITH-USERNAME> # Optional. Example: "admin"
  16. - name: password
  17. value: <REPLACE-WITH-PASSWORD> # Optional.
  18. - name: databaseName
  19. value: <REPLACE-WITH-DATABASE-NAME> # Optional. default: "daprStore"
  20. - name: collectionName
  21. value: <REPLACE-WITH-COLLECTION-NAME> # Optional. default: "daprCollection"
  22. - name: writeconcern
  23. value: <REPLACE-WITH-WRITE-CONCERN> # Optional.
  24. - name: readconcern
  25. value: <REPLACE-WITH-READ-CONCERN> # Optional.
  26. - name: operationTimeout
  27. 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
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”

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

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.

Last modified November 12, 2021 : Merge pull request #1949 from willtsai/az-staticwebapp-versioning (c40e456)