Introduction
Learn how to interact with MongoDB’s Data API to perform CRUD operations using RESTful endpoints.
Prerequisites
- Ensure you have MongoDB Atlas configured and your cluster set up.
- Obtain your MongoDB Data API service URL.
- Authenticate and authorize your API access.
Using MongoDB Data API
MongoDB’s Data API provides a RESTful interface to interact with your database collections. Here are common operations:
GET – Retrieve Documents
Example: Retrieving Documents
GET /api/atlas/v1.0/groups/{GROUP-ID}/clusters/{CLUSTER-NAME}/databases/{DATABASE-NAME}/collections/{COLLECTION-NAME}/documents
This endpoint retrieves all documents from the specified collection.
POST – Create Documents
Example: Creating Documents
POST /api/atlas/v1.0/groups/{GROUP-ID}/clusters/{CLUSTER-NAME}/databases/{DATABASE-NAME}/collections/{COLLECTION-NAME}/documents
This endpoint creates a new document in the specified collection.
PUT – Update Documents
Example: Updating Documents
PUT /api/atlas/v1.0/groups/{GROUP-ID}/clusters/{CLUSTER-NAME}/databases/{DATABASE-NAME}/collections/{COLLECTION-NAME}/documents/{DOCUMENT-ID}
This endpoint updates an existing document identified by {DOCUMENT-ID}
in the specified collection.
DELETE – Delete Documents
Example: Deleting Documents
DELETE /api/atlas/v1.0/groups/{GROUP-ID}/clusters/{CLUSTER-NAME}/databases/{DATABASE-NAME}/collections/{COLLECTION-NAME}/documents/{DOCUMENT-ID}
This endpoint deletes an existing document identified by {DOCUMENT-ID}
from the specified collection.
Handling Responses
Responses from MongoDB’s Data API typically include status codes and JSON-formatted data payloads. Handle these responses according to your application’s requirements.
Conclusion
The MongoDB Data API simplifies interaction with MongoDB databases through standard HTTP methods, allowing for seamless integration with web applications and services. Experiment with these endpoints to manage your MongoDB data effectively.