MongoDB Mongoose icon

MongoDB Mongoose

Interact with MongoDB using Mongoose ODM

Overview

This node allows users to interact with MongoDB databases using the Mongoose Object Data Modeling (ODM) library. It supports a variety of operations such as finding one document, finding multiple documents, creating, updating, deleting, counting, and running aggregation pipelines on collections within a MongoDB database.

The "Find One" operation specifically retrieves a single document from a specified collection that matches a given query filter. This is useful when you want to fetch a unique or first matching record based on certain criteria.

Common scenarios:

  • Retrieving user details by a unique identifier or email.
  • Fetching configuration or settings documents where only one record is expected.
  • Querying for a specific order or transaction in an e-commerce system.

Practical example:
You want to find a user document in the "users" collection where the username is "johndoe". You provide a query { "username": "johndoe" } and optionally specify which fields to return, such as 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.
Query A JSON object representing the MongoDB query filter to find the document. Leave empty for no filter (matches any document). Example: {"name": "John", "age": {"$gte": 18}}.
Schema Definition JSON defining the Mongoose schema for the collection. Specifies field names and their types (e.g., String, Number, Date). Required to define the structure for Mongoose.
Select Fields JSON string specifying which fields to include or exclude in the result. For example, {"_id": 1, "name": 1} includes only _id and name fields. Leave empty to select all fields.
Include Query Info Boolean flag indicating whether to include raw MongoDB query information in the response output. Useful for debugging or logging.
Options Additional options for the query execution:
- Limit Maximum number of results to return (not very relevant for "findOne" but available).
- Skip Number of documents to skip (not applicable for "findOne").
- Sort JSON specifying sort order (not applicable for "findOne").
- Select String specifying fields to include/exclude (alternative to Select Fields; not applicable for "findOne").
- Query Timeout (Ms) Maximum time in milliseconds to allow the query to run (between 1000 and 300000 ms).
- Debug Mode Enables Mongoose debug mode to log all MongoDB queries to the console.

Output

The output is an array of items, each containing a json property with the following structure for the "Find One" operation:

  • The main content is the found document represented as a JSON object with the requested fields.
  • If no document matches the query, the output will contain null as the result.
  • If "Include Query Info" is enabled, the output JSON also contains metadata about the executed query, including:
    • The operation type (findOne)
    • The original query object used
    • The options applied during the query (such as selected fields)

Example output JSON (simplified):

{
  "result": {
    "_id": "60d5ec49f8d2c4567a123456",
    "name": "John Doe",
    "email": "john@example.com"
  },
  "query": {
    "name": "John Doe"
  },
  "options": {
    "selectFields": {
      "_id": 1,
      "name": 1,
      "email": 1
    }
  },
  "operation": "findOne"
}

If no document is found, "result" will be null.

Dependencies

  • Requires a MongoDB instance accessible via a connection string.
  • Uses Mongoose ODM library to interact with MongoDB.
  • Requires an API key credential configured in n8n for MongoDB access.
  • Supports multi-database connections by specifying the database name or using the default from credentials.
  • The node expects a valid Mongoose schema definition for the target collection to properly parse and cast query parameters.

Troubleshooting

  • Connection errors:
    Errors like "MongoDB connection error", "host not found", or "connection refused" indicate issues with the connection string, network, or MongoDB server availability. Verify the connection string, hostname/IP, port, and ensure MongoDB is running.

  • Authentication failures:
    Messages mentioning authentication failure suggest incorrect username, password, or authentication database settings. Double-check credentials and permissions.

  • Invalid JSON format:
    Input properties such as Query, Schema Definition, or Select Fields must be valid JSON strings. Invalid JSON will cause parsing errors. Use proper JSON syntax.

  • 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 (not typical for "findOne"), a warning is logged suggesting adding indexes or refining filters.

  • Schema mismatch:
    Incorrect or incomplete schema definitions may cause unexpected behavior or casting errors. Ensure the schema matches the actual collection structure.

  • Timeouts:
    Queries taking longer than the specified timeout (default 30 seconds) will fail. Adjust the "Query Timeout" option if necessary.

Links and References

Discussion