MongoDB

Detailed information on the MongoDB state store component

Setup a MongoDB state store

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 will install 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 will be admin by default.

Create a Dapr component

The next step is to create a Dapr component for MongoDB.

Create the following YAML file named mongodb.yaml:

  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.

Example

The following example uses the Kubernetes secret store to retrieve the username and password:

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: <NAME>
  5. namespace: <NAMESPACE>
  6. spec:
  7. type: state.mondodb
  8. version: v1
  9. metadata:
  10. - name: host
  11. value: <REPLACE-WITH-HOST>
  12. - name: username
  13. secretKeyRef:
  14. name: <KUBERNETES-SECRET-NAME>
  15. key: <KUBERNETES-SECRET-KEY>
  16. - name: password
  17. secretKeyRef:
  18. name: <KUBERNETES-SECRET-NAME>
  19. key: <KUBERNETES-SECRET-KEY>
  20. ...

Apply the configuration

In Kubernetes

To apply the MondoDB state store to Kubernetes, use the kubectl CLI:

  1. kubectl apply -f mongodb.yaml

Running locally

To run locally, create a components dir containing the YAML file and provide the path to the dapr run command with the flag --components-path.

Last modified February 16, 2021: Merge pull request #1235 from dapr/update-v0.11 (b4e9fbb)