mongo.connector
Overview
The mongo.connector is a vital component of Vantage that enables users to connect to and interact with MongoDB databases seamlessly. It allows for read operations on specified collections within MongoDB databases while enforcing authentication and managing secure credential retrieval. The logic is designed for environments utilizing the database as the ORM for database interactions and the authentication system for user session management.
Purpose
The primary purpose of the mongo.connector is to facilitate secure and authorized access to MongoDB collections. It ensures that only logged-in users can make queries against the MongoDB service, thereby maintaining secure data access controls.
How It Works
- The connector checks if the user is authenticated by fetching the session through the server session.
- It retrieves the
clientIdfrom the session and ensures that it is valid. - The connector retrieves the service key for the MongoDB adapter from the database.
- If the service is found, it fetches the stored credentials for accessing the MongoDB database.
- It performs the specified operation (defaulting to 'find') on the requested collection, applying any filters or projections as provided in the inputs.
- It handles errors gracefully, providing descriptive error messages when issues arise during database interactions.
Settings
The mongo.connector accepts the following settings as part of the inputs parameter:
1. Collection
- Name:
collection - Input Type: String
- Description: This setting specifies the MongoDB collection from which data will be retrieved. It is mandatory for the connector to function properly. If omitted, an error is thrown.
- Default Value: None (required).
2. Operation
- Name:
operation - Input Type: String
- Description: This setting dictates the type of database operation to perform. The default operation is 'find', which retrieves documents from the specified collection. Other MongoDB operations can be incorporated, but they are not defined in this implementation.
- Default Value: ‘find’.
3. Filter
- Name:
filter - Input Type: Object
- Description: This setting allows users to define a filter for the database query. It determines which documents will be retrieved from the specified collection based on specified criteria. If not provided, all documents will be retrieved.
- Default Value: {} (empty object, meaning no filtering).
4. Projection
- Name:
projection - Input Type: Object
- Description: This setting allows users to specify which fields to include or exclude in the result set. It helps to limit the amount of data returned, enhancing performance and readability. If omitted, all fields will be included in the output.
- Default Value: {} (empty object, meaning all fields).
Use Cases & Examples
Use Case 1: Data Analysis
A data analyst requires access to specific fields in a MongoDB collection to generate reports. By utilizing the mongo.connector, the analyst can run queries with custom projections to extract only the necessary data.
Use Case 2: Application Backend
A backend service for an e-commerce application needs to fetch user order details from a MongoDB database. The mongo.connector can be configured to retrieve this data based on specific user IDs and order statuses efficiently.
Use Case 3: Dynamic Filtering
An admin dashboard requires dynamic dashboards that leverage MongoDB collections to show user activity. The mongo.connector can be configured to retrieve filtered user activity logs based on various parameters like date ranges and activity types.
Example Configuration
Consider the scenario where a backend service needs to fetch active orders for a specific user from a collection named orders. The desired operation is find with a specific filter and projection.
const inputs = {
collection: 'orders',
operation: 'find',
filter: {
userId: '12345',
status: 'active',
},
projection: {
orderId: 1,
orderDate: 1,
total: 1,
},
};In this configuration:
- The
collectionis set to'orders', specifying which MongoDB collection to query. - The
operationis the default'find', which will retrieve documents matching the filter. - The
filtertargets orders belonging to a user with ID'12345'that have a status of'active'. - The
projectionlimits the returned fields toorderId,orderDate, andtotal, ensuring that only relevant data is retrieved.
This query effectively retrieves all active orders for the particular user while maintaining efficient data handling and security.