MongoDB Mongoose icon

MongoDB Mongoose

Interact with MongoDB using Mongoose ODM

Overview

This node allows interaction with MongoDB databases using the Mongoose Object Data Modeling (ODM) library. It supports various operations such as counting documents, finding documents, creating new documents, updating existing ones, deleting documents, and running aggregation pipelines on a specified collection within a MongoDB database.

A common use case is when you want to perform complex queries or data manipulations on MongoDB collections directly from an n8n workflow without writing custom code for connection handling or query execution. For example, you can count how many users meet certain criteria, update multiple records based on a filter, or aggregate sales data by region.

Specifically, the Count operation counts the number of documents in a collection that match a given query filter.

Properties

Name Meaning
Database The name of the MongoDB database to connect to. Leave empty to use the default database from credentials.
Collection The name of the collection on which to perform the operation (required).
Query A JSON object representing the MongoDB query filter to select documents. Leave empty ({}) for no filter.
Schema Definition JSON defining the Mongoose schema for the collection, specifying field names and their types (required).
Include Query Info Boolean flag to include raw MongoDB query information in the output response.
Options A collection of additional options:
- Limit Maximum number of results to return (default 1000, max 10,000).
- Skip Number of documents to skip (not applicable for aggregate operations).
- Sort JSON object defining sort order (e.g., {"name": 1, "age": -1}), not applicable for aggregate operations.
- Select Fields String listing fields to include or exclude (e.g., "name email -_id"), not applicable for aggregate operations.
- Upsert Whether to create a document if it doesn't exist (only for update operations).
- Multi Whether to update multiple documents (only for update operations).
- Query Timeout (Ms) Maximum time allowed for the query to run, between 1000 ms and 300000 ms (default 30000 ms).
- Debug Mode Enables Mongoose debug mode to log all MongoDB queries to the console.

Output

The output JSON structure depends on the operation but for the Count operation specifically:

  • The main output is a single number representing the count of documents matching the query.
  • If the "Include Query Info" option is enabled, the output will also include metadata about the executed query and options used.
  • The output is wrapped in an object with keys like data (the count result) and optionally query and options if query info is included.

Example output JSON for Count operation without query info:

{
  "data": 42
}

With query info enabled:

{
  "data": 42,
  "query": { /* the parsed query object */ },
  "options": { /* options used for the count operation */ }
}

No binary data is produced by this node.

Dependencies

  • Requires a valid MongoDB connection string and credentials configured in n8n.
  • Uses the Mongoose ODM library internally to interact with MongoDB.
  • The node expects a properly defined Mongoose schema for the target collection.
  • Network access to the MongoDB server must be available.
  • Optional: Enabling debug mode requires console access to view logs.

Troubleshooting

  • Connection errors: Messages like "MongoDB connection error", "host not found", or "connection refused" indicate network or configuration issues. Verify the connection string, hostname/IP, port, and ensure MongoDB is running and accessible.
  • Authentication failures: Errors mentioning authentication failure suggest incorrect username, password, or authentication database settings.
  • Invalid JSON: The node parses JSON inputs for queries and schemas. Invalid JSON strings will cause errors. Ensure JSON syntax is correct.
  • Limit exceeded: Setting a limit above 10,000 will throw an error for safety reasons.
  • Large result sets warning: When retrieving large numbers of documents (>5000), a warning is logged suggesting adding indexes or refining filters.
  • Timeouts: Queries taking longer than the specified timeout (default 30 seconds) may fail. Adjust the "Query Timeout" option if needed.
  • Schema mismatch: Incorrect schema definitions may cause unexpected behavior or errors during query parsing.

Links and References

Discussion