GitHub

RDK

MongoDB

MongoDB

rdk.mongodb allows developers to:

  1. Connect to a MongoDB database using a URI.
  2. Perform operations on a specific collection.
  3. Utilize MongoDB CRUD and aggregation operations in a structured manner.

Version Compatibility: Compatible with RIO version 2.1.21 and above. and RDK 2.0.27 and above

Operation Exclusivity: Only one operation (e.g., find, insertOne) should be provided per request.


Function Signature

rdk.mongodb(request: MongoRequest): Promise<MongoResponse>

MongoRequest

An object specifying the MongoDB operation and its parameters.

MongoResponse

A promise resolving to the result of the operation or an error if the operation fails.


Request Parameters

Common Parameters

ParameterTypeDescriptionRequired
uristringMongoDB connection URI.Yes
databasestringThe database name to perform the operation on.Yes
collectionstringThe collection name to perform the operation on.Yes

Supported Operations

Each operation accepts specific fields. Below are details about the operations and their parameters:

Find Operations

OperationInterfaceParameters
findMongoFindfilter, skip, limit, options
findOneBaseMongoQueryfilter, options

Update Operations

OperationInterfaceParameters
updateOneMongoQueryWithUpdatefilter, update, options (supports upsert)
updateManyMongoQueryWithUpdatefilter, update, options (supports upsert)
findOneAndUpdateMongoQueryWithUpdatefilter, update, options (e.g., returnDocument, upsert)

Insert Operations

OperationInterfaceParameters
insertOneMongoInsertOnedocument, options
insertManyMongoInsertManydocuments, options

Delete Operations

OperationInterfaceParameters
deleteOneBaseMongoQueryfilter, options
deleteManyBaseMongoQueryfilter, options
findOneAndDeleteBaseMongoQueryfilter, options

Advanced Operations

OperationInterfaceParameters
aggregateMongoAggregatepipeline, options
countDocumentsMongoCountDocumentsfilter, options
distinctMongoDistinctkey, filter

Example Usage

Insert a Document

const insertOneResult = await rdk.mongodb({
    uri: MONGO_DB_URI,
    database: 'admin',
    collection: 'users',
    insertOne: {
        document: {
            name: 'John Doe',
            email: 'john.doe@example.com',
        },
    },
});

Update a Document (Upsert)

const updateOneResult = await rdk.mongodb({
    uri: MONGO_DB_URI,
    database: 'admin',
    collection: 'users',
    updateOne: {
        filter: { email: 'john.doe@example.com' },
        update: { $set: { name: 'John Updated' } },
        options: { upsert: true },
    },
});

Query Multiple Documents

const findResult = await rdk.mongodb({
    uri: MONGO_DB_URI,
    database: 'admin',
    collection: 'users',
    find: {
        filter: { isActive: true },
        limit: 10,
    },
});

Perform Aggregation

const aggregateResult = await rdk.mongodb({
    uri: MONGO_DB_URI,
    database: 'admin',
    collection: 'orders',
    aggregate: {
        pipeline: [
            { $match: { status: 'shipped' } },
            { $group: { _id: '$customerId', totalOrders: { $sum: 1 } } },
        ],
    },
});

Count Documents

const countResult = await rdk.mongodb({
    uri: MONGO_DB_URI,
    database: 'admin',
    collection: 'users',
    countDocuments: {
        filter: { isActive: true },
    },
});

Error Handling

Response Structure

All operations return a structured response:

interface MongoResponse {
    success: boolean;
    data?: any;
    error?: any;
}

Example

if (!response.success) {
    console.error('Error:', response.error);
}

For additional questions or troubleshooting, refer to the RDK MongoDB documentation or contact support.

Previous
Static IP