Overview
This node enables interaction with MongoDB databases using the Mongoose Object Data Modeling (ODM) library. It supports a variety of operations such as finding one or multiple documents, creating, updating, deleting documents, counting documents, and running aggregation pipelines on specified collections within a MongoDB database.
The "Find One" operation specifically retrieves a single document from a collection that matches a given query filter. This is useful when you want to fetch a unique record or the first matching record based on certain criteria.
Common scenarios:
- Retrieving user details by a unique identifier or email.
- Fetching configuration or settings stored in a MongoDB collection.
- Querying for a single document to check existence or get detailed information.
Practical example:
You have a users collection and want to find a user whose name is "John" and age is at least 18. You can specify a query like:
{
"name": "John",
"age": { "$gte": 18 }
}
and retrieve only selected fields such as _id, name, and email.
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 to operate on (required). |
| Query | MongoDB query object in JSON format to filter documents. Leave empty {} for no filter. |
| Schema Definition | JSON definition of the Mongoose schema for the collection. Defines field types and structure (required). |
| Select Fields | JSON string specifying which fields to include or exclude in the result. Leave empty to select all fields. Example: {"_id": 1, "name": 1} |
| Include Query Info | Boolean flag indicating whether to include raw MongoDB query information in the response. |
| Options | Additional options including: |
| - Limit | Maximum number of results to return (not applicable for "findOne"). |
| - Skip | Number of documents to skip (not applicable for "findOne"). |
| - Sort | JSON object defining sort order (e.g., {"name": 1, "age": -1}) (not applicable for "findOne"). |
| - Select | String listing fields to include/exclude (e.g., "name email -_id") (not applicable for "findOne"). |
| - Query Timeout (Ms) | Maximum time in milliseconds to allow the query to run (1,000 to 300,000 ms). |
| - Debug Mode | Enables Mongoose debug mode to log all MongoDB queries in the console. |
Output
The output is an array of items where each item contains a json property with the following structure for the "Find One" operation:
- The main content is the found document represented as a plain JSON object with the fields selected according to the input parameters.
- If no document matches the query, the output will be
null. - If "Include Query Info" is enabled, the output JSON also includes metadata about the executed query and options used.
Example output JSON:
{
"data": {
"_id": "60d5ec49f8d2c4567a123456",
"name": "John",
"email": "john@example.com",
"age": 25
},
"query": {
"name": "John",
"age": { "$gte": 18 }
},
"options": {
"limit": 1000,
"skip": 0,
"sort": {},
"selectFields": { "_id": 1, "name": 1, "email": 1 }
},
"operation": "findOne"
}
No binary data output is produced by this operation.
Dependencies
- Requires a MongoDB instance accessible via a connection string.
- Uses Mongoose ODM library for schema definition and querying.
- Requires an API key credential or similar authentication token configured in n8n to connect to MongoDB.
- Supports multi-database connections by specifying the database name or using the default from credentials.
Troubleshooting
Connection errors:
Errors like "MongoDB connection error", "connection refused", or "host not found" indicate issues with the connection string, network, or MongoDB server availability. Verify the connection string, hostname/IP, port, and ensure MongoDB is running and reachable.Authentication failures:
Messages mentioning "authentication failed" suggest incorrect username, password, or authentication database settings. Double-check credentials and authentication parameters.Query parsing errors:
Invalid JSON in the "Query" or "Select Fields" properties will cause errors. Ensure these inputs are valid JSON strings.Limit exceeded:
The node enforces a maximum limit of 10,000 documents for safety. Setting a higher limit will throw an error.Large result sets warning:
When retrieving many documents (more than 5,000), a console warning suggests adding indexes or refining filters to improve performance.Schema mismatch:
Incorrect schema definitions may cause unexpected behavior. Make sure the schema JSON correctly reflects the collection's document structure.
Links and References
- Mongoose Documentation โ For schema definitions and querying.
- MongoDB Query Operators โ To build query objects.
- MongoDB Aggregation Pipeline โ For advanced data processing (other operations).
- n8n Documentation โ General guidance on using nodes and credentials.