Working with MongoDB databases

To work with your MongoDB instance on Cloud Pak for Data, you can use any standard MongoDB client.

Before you begin

Before you work with a MongoDB database on Cloud Pak for Data, you need to configure port forwarding so you can retrieve your MongoDB database from a client you can use to work with the database.

Tip: The examples below use MongoDB Shell as a client for working with the MongoDB database on Cloud Pak for Data. For guidance on installing MongoDB Shell, see Install mongosh in the MongoDB documentation.

Follow these steps to set up access to the MongoDB database for the client.

  1. Configure port forwarding to connect to your MongoDB instance. Port forwarding is a convenient method to expose the MongoDB port to the client.
    1. Run the following command to get the details about your MongoDB instance:
      kubectl get service <instance_name>-mongodb-svc -n ${PROJECT_CPD_INST_OPERANDS}

      Where <instance_name> is the name of your MongoDB instance, and PROJECT_CPD_INST_OPERANDS is the namespace where the MongoDB service is deployed.

      The command will generate an output similar to the following example:
      
      NAME                         TYPE        CLUSTER-IP   EXTERNAL-IP   PORT(S)     AGE
      <instance_name>-mongodb-svc  ClusterIP   None         <none>        27017/TCP   89m
    2. Verify that the MongoDB Server is running in the pod and is retrievable from the port.
      1. Run the following command to retrieve the pod:
        % oc get pod
        The output generated will be similar to the following example:
        
        NAME                                  READY        STATUS       RESTARTS      AGE
        <instance_name>-mongodb-0             1/1          Running      0             31m
      2. Run the following command to view the port connected to the MongoDB instance:
        kubectl get pod <instance_name>-mongodb-0 --template='{{(index (index .spec.containers 0).ports 0).containerPort}}{{"\n"}}'
        The output generated is the port connected to the MongoDB instance.
        27017
    3. Run the command to assign a local port to your MongoDB instance. For example, run:
      % kubectl port-forward service/<instance_name>-mongodb-svc 28015:27017 
      The command will generate an output similar to the following example:
      Forwarding from 127.0.0.1:28015 -> 27017
      
      Forwarding from [::1]:28015 -> 27017
  2. Connect to your MongoDB instance by using the port you just configured. Open a new terminal window and run a MongoDB Shell.
    # mongosh --port 28015
    The output will be similar to the following example:
    Current Mongosh Log ID:        633df75bcc34d07b1f7eea44
    Connecting to:                mongodb://127.0.0.1:28015/?directConnection=true&serverSelectionTimeoutMS=2000&appName=mongosh+1.6.0
    Using MongoDB:                4.4.0
    Using Mongosh:                1.6.0
    Enterprise mdb33-mongodb [direct: primary] test>
    Enterprise <instance-name>-mongodb [direct]

MongoDB operations

After you configure port forwarding, you can do any of these operations with your MongoDB client. The following instructions use MongoDB Shell as an example.

Operation Description Instructions
Insert document Insert a single document in a collection. Modify the following command example to insert a document:
db.inventory.insertOne(

   { item: "canvas", qty: 100, tags: ["cotton"], size: { h: 28, w: 35.5, uom: "cm" } }

)
Retrieve document Retrieve a document that has been inserted into a collection. Modify the following command example to retrieve a document:
db.inventory.find( { item: "canvas" } )
The following output will result:
Enterprise mdb33-mongodb [direct: primary] admin> db.inventory.find( { item: "canvas" } )

[

  {

    _id: ObjectId("633dfc17a6be8d3c4f55e019"),

    item: 'canvas',

    qty: 100,

    tags: [ 'cotton' ],

    size: { h: 28, w: 35.5, uom: 'cm' }

  }

]
Delete all documents Delete all documents from a collection. Run the following command to delete all documents:
db.inventory.deleteMany({})
The following output will result:
admin> db.inventory.deleteMany({})

{ acknowledged: true, deletedCount: 1 }

Deleting a MongoDB instance

A user with the manage service instances permission can delete a MongoDB instance. To delete an instance:

  1. From the Cloud Pak for Data navigation menu, select Services > Instances.
  2. Filter the list by Type.
  3. Delete the MongoDB instance.