Skip to main content

03.02 Integration with Mongo DB Atlas

import { MongoClient } from 'mongodb';

export default async function run({execution_id, input, data}) {

// MongoDB Connection String
// Structure:
// mongodb+srv://USERNAME:PASSWORD@CLUSTER_ADDRESS/DATABASE
// USERNAME: User's name for authentication
// PASSWORD: Password for authentication
// CLUSTER_ADDRESS: Address to your MongoDB cluster
// DATABASE: Default database to use (optional)
const connectionString = 'mongodb+srv://USERNAME:PASSWORD@CLUSTER_ADDRESS/DATABASE';

// Connecting to the MongoDB client
const client = await MongoClient.connect(
connectionString,
{ useNewUrlParser: true, useUnifiedTopology: true } // Options for the connection
);

// Selecting the 'parsing-m' database and 'apps' collection
const coll = client.db('parsing-m').collection('apps');

// Finding the last 3 records where processed is not equal to true
const filter = {processed:{$ne: true}};
// Sorting by the "sort" field in descending order (replace "sort" with the actual field name)
const cursor = coll.find(filter).sort({"sort": -1}).limit(3);

// Converting the cursor to an array to retrieve the result
const result = await cursor.toArray();

// Closing the connection to the MongoDB client
await client.close();

return result;
}