Skip to main content
Create your own
Lesson illustration

NoSQL CRUD Operations

Hello! Welcome to your next lesson in our "Working with Databases" module.

In the previous lessons, we focused exclusively on relational databases using SQL. You learned how to perform Create, Read, Update, and Delete (CRUD) operations, and you finished by mastering the critical skill of using parameterized queries to secure your workflows against SQL injection.

Today, we're shifting gears from the structured world of SQL to the flexible domain of NoSQL databases. We will focus on MongoDB, one of the most popular NoSQL databases, which is widely used in modern web applications and is particularly well-suited for the kinds of AI-driven projects you're interested in. Its document-based structure, which uses JSON-like objects, will feel very natural given your background as a software developer.

By the end of this lesson, you will be able to perform CRUD operations on a NoSQL database (e.g., MongoDB) using n8n's dedicated nodes.

1. Setting up your MongoDB Atlas Database

Before we can interact with MongoDB in n8n, we need a database. We will use MongoDB Atlas, the official cloud-hosted version, which offers a generous free tier perfect for development.

The setup process involves several steps:

  1. Creating a free MongoDB Atlas account and a new cluster.
  2. Configuring network access to allow connections to your cluster.
  3. Creating a database user with the necessary permissions.
  4. Getting the connection string required by n8n.
  5. Creating a new database and collection within Atlas.
  6. Adding the MongoDB credentials to your n8n instance.

The following video provides a clear, step-by-step guide through this entire process.

How to Connect MongoDB to n8n: Step-by-Step Guide

This video from Phylux Express will walk you through the entire setup from creating a MongoDB Atlas account to successfully connecting it in n8n.

Please watch the video from 00:40 to 08:32. Follow the steps shown to set up your own free cluster and configure the credential in your n8n workspace. Don't worry about creating a workflow just yet; simply ensure your credential connects successfully.

Important Security Note: In the video (at 02:40), the presenter allows access from any IP address (0.0.0.0/0) for simplicity. For a real project, you would want to restrict this to specific IP addresses for security. For this tutorial, allowing access from anywhere is acceptable.

Once you have successfully saved your MongoDB credential in n8n, you're ready to start building.

2. CRUD Operations with the MongoDB Node

Now that your credentials are set up, let's explore how to perform the four fundamental database operations: Create, Read, Update, and Delete. We'll build a single workflow to demonstrate each one.

Start by creating a new, blank workflow.

Create: Inserting a Document

First, we'll insert a new document into our collection. In MongoDB, a document is essentially a JSON object. A good practice, just as with SQL, is to prepare your data before sending it to the database.

  1. Add a Set node to your canvas. Connect it to the Start node. We'll use this to define the document we want to create.
    • Set Mode to Append.
    • Add the following values to create a simple user profile:
      • Name: name, Value: Alice
      • Name: email, Value: alice@example.com
      • Name: status, Value: active
  2. Add a MongoDB node.
    • Credential for MongoDB: Select the credential you just created.
    • Operation: Choose Insert.
    • Collection: Enter the name of the collection you created in MongoDB Atlas (e.g., first-data-collection).
    • Document to Insert: This field defaults to {{ $json }}. This expression tells the node to take the entire JSON object from the previous node (our Set node) and insert it as a new document. Leave this as is.

Execute the workflow. You should see a successful execution. You can log into your MongoDB Atlas account, navigate to your collection, and see the newly created document for Alice. Notice that MongoDB has automatically added a unique _id field to the document.

Read: Finding a Document

Next, let's retrieve the document we just created.

  1. Add another MongoDB node and connect it to the Set node (you can drag a new connection from the Set node's output).
  2. Configure the node:
    • Operation: Choose Find.
    • Collection: Enter your collection name again.
    • Query: This is where you define your search criteria, using MongoDB's JSON-based query language. To find Alice by her email, enter: {"email": "alice@example.com"}

Execute this node. The output will be an array containing the full document for Alice, including the all-important _id field.

Update: Modifying a Document

Now, let's change Alice's status from active to inactive. The key to updating a specific document is using its unique _id.

The following blog post provides an excellent, practical guide to CRUD operations, with a particularly clear explanation of how to handle the _id for updates.

How to use MongoDB in n8n

This article from indiantinker's blog, 'How to use MongoDB in n8n', is a great resource. We'll focus on its clear explanations of the CRUD operations.

Read the sections 'The _id Field' and 'Update - Modify Documents'. Pay close attention to how the _id from a Find operation is used as the 'Update Key' in the Update operation. You don't need to read the full article right now.

Let's apply this. Our workflow will now look like this: Set -> Find -> Update.

  1. Connect a new MongoDB node to the output of your Find node.
  2. Configure the node:
    • Operation: Update.
    • Collection: Your collection name.
    • Update Key: This tells MongoDB which field to use to identify the document to update. Enter _id.
    • Fields to Update: Here you specify the fields you want to change. Add one field:
      • Key: status, Value: inactive.

Now, n8n will take the _id from the output of the Find node and use it to locate the correct document. Then, it will apply the update, changing the status field to inactive. Execute the node and verify the change in MongoDB Atlas.

Test your understanding!

You have a Find node that outputs a document with an _id of {"$oid": "60d5ec49e9c4b3001a1b2c3d"} and an email of test@example.com. You want to update this user's name to "Bob" using an Update node. What should you set as the Update Key?

A) email
B) _id
C) {"$oid": "60d5ec49e9c4b3001a1b2c3d"}

Show answer

B) _id is correct. The Update Key field asks for the name of the field to match against. n8n will automatically take the value of the _id field from the incoming item (which is the full {"$oid": ...} object) and use it for the update. email would also work if it's unique, but using the primary _id is the most direct and reliable method.

Delete: Removing a Document

Finally, let's remove the document. This operation is permanent, so it's crucial to be certain about your query.

  1. Add one last MongoDB node. You can connect it after the Update node for a full sequence, or connect it directly to the Find node.
  2. Configure it:
    • Operation: Delete.
    • Collection: Your collection name.
    • Query: Just like the Find operation, this defines which documents to delete. To delete Alice's record, you can use the same query: {"email": "alice@example.com"}

A crucial best practice: Before ever running a Delete operation in a production workflow, always test the exact same query in a Find node first to ensure you are targeting only the documents you intend to remove.

Execute the Delete node. If you check MongoDB Atlas now, Alice's document will be gone.

3. Context: CRUD in AI Workflows

These basic CRUD operations are the building blocks for much more complex and interesting workflows, especially those involving AI, which is a key goal for you.

For instance, an AI agent might need to Find information to answer a user's question or Insert data to remember a user's preferences.

AI Show - n8n.io platform for AI workflows and MongoDB integration

To see this in action, let's look at a short clip from the 'AI Show' by MongoDB. Here, the presenter builds a workflow where an AI agent needs to save a user's 'favorite' movie to the database.

Watch from 37:46 to 40:20. Notice how the core task of saving the movie is handled by a separate sub-workflow that performs a simple Insert operation, just like the one you built.

This clip shows that even in an advanced AI application, the fundamental database interactions rely on the CRUD principles you've learned today. It also gives a sneak peek at a concept we'll cover in our next lesson: sub-workflows.

Conclusion

Congratulations! You have successfully expanded your database skills from the relational world of SQL into the non-relational world of NoSQL with MongoDB.

Key Takeaways:

  • NoSQL & MongoDB: MongoDB is a document database that stores data in flexible, JSON-like documents. This is different from the rigid table structure of SQL databases.
  • Connecting to Atlas: You can connect n8n to a cloud-hosted MongoDB Atlas database by creating a cluster, user, and using the connection string to create an n8n credential.
  • The MongoDB Node: n8n provides a powerful MongoDB node with operations for Insert (Create), Find (Read), Update, and Delete.
  • The _id field: This unique identifier, automatically added by MongoDB, is the most reliable key for Update and Delete operations.
  • Queries are JSON: Unlike SQL's text-based queries, MongoDB queries are written as JSON objects that specify the criteria for finding, updating, or deleting documents.

This lesson completes our module on "Working with Databases." You are now equipped to connect and manipulate data in both major types of databases used in modern development.

Preview of the Next Lesson:
In our next module, "Modular and Reusable Workflows," we will zoom out from individual nodes and focus on workflow architecture. We'll start by learning how to create and call sub-workflows using the Execute Workflow node, a technique you saw briefly in the final video. This allows you to encapsulate logic (like a database insert) into a reusable component, making your workflows cleaner, more organized, and easier to maintain.

Can't find a good explanation? Sign up and we'll make it for you

Sign up