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.
mongosh
in the MongoDB
documentation.Follow these steps to set up access to the MongoDB database for the client.
- Configure port forwarding to connect to your MongoDB instance. Port forwarding is a
convenient method to expose the MongoDB port to the client.
- 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
- Verify that the MongoDB Server is running in
the pod and is retrievable from the port.
- Run the following command to retrieve the
pod:
The output generated will be similar to the following example:% oc get pod
NAME READY STATUS RESTARTS AGE <instance_name>-mongodb-0 1/1 Running 0 31m
- Run the following command to view the port connected to the MongoDB
instance:
The output generated is the port connected to the MongoDB instance.kubectl get pod <instance_name>-mongodb-0 --template='{{(index (index .spec.containers 0).ports 0).containerPort}}{{"\n"}}'
27017
- Run the following command to retrieve the
pod:
- Run the command to assign a local port to your MongoDB instance. For example,
run:
The command will generate an output similar to the following example:% kubectl port-forward service/<instance_name>-mongodb-svc 28015:27017
Forwarding from 127.0.0.1:28015 -> 27017 Forwarding from [::1]:28015 -> 27017
- Run the following command to get the details about your MongoDB
instance:
- Connect to your MongoDB instance by using the port you just configured. Open a new
terminal window and run a MongoDB
Shell.
The output will be similar to the following example:# mongosh --port 28015
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:
|
Retrieve document | Retrieve a document that has been inserted into a collection. | Modify the following command example to retrieve a
document: The
following output will
result:
|
Delete all documents | Delete all documents from a collection. | Run the following command to delete all
documents: The
following output will
result:
|
Deleting a MongoDB instance
A user with the manage service instances permission can delete a MongoDB instance. To delete an instance:
- From the Cloud Pak for Data navigation menu, select .
- Filter the list by Type.
- Delete the MongoDB instance.