4 min read

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

  1. The connector checks if the user is authenticated by fetching the session through the server session.
  2. It retrieves the clientId from the session and ensures that it is valid.
  3. The connector retrieves the service key for the MongoDB adapter from the database.
  4. If the service is found, it fetches the stored credentials for accessing the MongoDB database.
  5. It performs the specified operation (defaulting to 'find') on the requested collection, applying any filters or projections as provided in the inputs.
  6. 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

2. Operation

3. Filter

4. Projection

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.

javascript
const inputs = {
  collection: 'orders',
  operation: 'find',
  filter: {
    userId: '12345',
    status: 'active',
  },
  projection: {
    orderId: 1,
    orderDate: 1,
    total: 1,
  },
};

In this configuration:

This query effectively retrieves all active orders for the particular user while maintaining efficient data handling and security.