The MongoDB API provides powerful capabilities to interact with a MongoDB database, allowing you to perform CRUD (Create, Read, Update, Delete) operations, manage databases, and execute sophisticated queries. With Pipedream, you can harness these abilities to automate tasks, sync data across various apps, and react to events in real-time. It’s a combo that’s particularly potent for managing data workflows, syncing application states, or triggering actions based on changes to your data.
import mongodb from 'mongodb'
export default defineComponent({
props: {
mongodb: {
type: "app",
app: "mongodb",
},
collection: {
type: "string"
},
filter: {
type: "object"
}
},
async run({steps, $}) {
const MongoClient = mongodb.MongoClient
const {
database,
hostname,
username,
password,
} = this.mongodb.$auth
const url = `mongodb+srv://${username}:${password}@${hostname}/test?retryWrites=true&w=majority`
const client = await MongoClient.connect(url, {
useNewUrlParser: true,
useUnifiedTopology: true
})
const db = client.db(database)
const results = await db.collection(this.collection).find(this.filter).toArray();
$.export('results', results);
await client.close()
},
})
Real-time Data Syncing with Google Sheets: Sync new MongoDB document entries to a Google Sheet for easy sharing and reporting. Whenever a new customer is added to the MongoDB database, a Pipedream workflow triggers and appends the customer data to a Google Sheet, keeping your sales team updated in real-time.
Automated Backup Notifications via Email: Set up a daily database backup and use Pipedream to monitor the completion status of the backup operation. Once a backup is successfully completed, trigger an SMTP action to send an email notification to the system admin, ensuring peace of mind with regular backup status updates.
Slack Alerts for High-Value Transactions: Monitor your MongoDB for transactions exceeding a certain value and send alerts to a Slack channel when such transactions occur. Finance teams can stay on top of significant movements without manual database checks, improving response time and financial oversight.
To get started, you need to set up a user to connect to your MongoDB cluster. This guide assumes your MongoDB cluster is hosted on Mongo Atlas.
First, log in to Mongo Atlas to complete the next steps.
First, create a dedicated user for connecting MongoDB to Pipedream.
Select the MongoDB project where your cluster resides, then select the Database Access tab. Here, you'll see a list of users who can connect to your MongoDB cluster.
Click Create new database user to get started.
In the new pop-up, choose the Password authentication method first. Name the user pipedream
so it’s easy to remember which service this user account is used for.
Generate a password and select a pre-built or custom role. Pre-built roles offer options like read-only or read and write access to all databases in the cluster. For more fine-tuned control, like restricting Pipedream to reading a single database, use a custom role.
Finally click Add user to create this new user account.
Now, with a dedicated MongoDB user, connect to Pipedream. Return to Pipedream to set up a new MongoDB action in a workflow or to add your MongoDB account in the Connected Accounts area.
Copy and paste the Username and Password fields from the previous step, when you created the pipedream
user in Mongo Atlas.
To find your database hostname, go to the Databases view in Mongo Atlas and click the Connect button for your chosen database.
Select the Compass or Node.js option. Then you’ll see the connection URI string. The hostname is the latter portion of the string, as shown below:
Lastly, provide the Database name to connect to.
By default, Pipedream workflows can start anywhere within the us-east-1
AWS IP range. You'll need to expose your MongoDB Atlas Cluster to the internet or use a Pipedream VPC to assign a specific outbound IP address to your workflows.
First, open the Network Access section of your MongoDB Atlas project. Then click Add new IP address in the top right-hand corner:
Then enter 0.0.0.0/0
in the IP address field to allow connections from any IP address. In the description field, we recommend adding a note about this requirement for Pipedream.
Finally, click Confirm
Pipedream VPCs allow you to define a single static IP address for your workflows and allow you to run these workflows in a separate network.
Follow this guide to create a VPC and assign the IP address to your workflow.
Then create a new allowed IP address in MongoDB by opening the Network Access section in Atlas:
![Add a new IP address to the allowed list of IP addresses within MongoDB Atlas](https://res.cloudinary.com/pipedreamin/image/upload
Create a new document in a collection of your choice. See the docs here
Execute an aggregation pipeline on a MongoDB collection. See the documentation
MongoDB uses API keys for authentication. When you connect your MongoDB account, Pipedream securely stores the keys so you can easily authenticate to MongoDB APIs in both code and no-code steps.
Before you connect to your Mongo database from Pipedream, please make sure your database is accessible from the public internet. You may need to add a firewall rule allowing connections from 0.0.0.0/0
.
Then, enter the details of your Mongo host / database below.