Kingfoodmart Mongoose icon

Kingfoodmart Mongoose

Interact with MongoDB using Mongoose ODM

Overview

This node enables interaction with MongoDB databases using the Mongoose Object Data Modeling (ODM) library. It supports various database operations such as counting documents, finding documents, creating new entries, updating existing ones, deleting documents, and running aggregation pipelines. The node is useful for workflows that require flexible and powerful querying or manipulation of MongoDB collections with schema validation.

Typical use cases include:

  • Counting how many documents match a certain filter.
  • Retrieving documents based on complex queries.
  • Updating multiple or single documents conditionally.
  • Deleting documents from a collection.
  • Running aggregation pipelines to transform or summarize data.

For example, you could count all users over 18 years old in a "users" collection or update the status field of multiple orders in an "orders" collection.

Properties

Name Meaning
Database Name of the MongoDB database to connect to. Leave empty to use the default database from credentials.
Collection Name of the MongoDB collection to operate on.
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, specifying field types (e.g., String, Date).
Include Query Info Boolean flag to include raw MongoDB query information in the response for debugging or logging purposes.
Options A collection of additional options:
- Limit Maximum number of results to return (1 to 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 Space-separated string of fields to include or exclude (e.g., "name email -_id"). Not for aggregate ops.
- Upsert Boolean indicating whether to create a document if it doesn't exist (only for update operations).
- Multi Boolean indicating whether to update multiple documents (only for update operations).
- 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 where each item contains a json property representing the result of the operation:

  • For Count operation:
    The output JSON contains the count number of documents matching the query, optionally wrapped with query info if enabled.

  • For other operations (not requested here but relevant):
    The output may contain arrays of documents, single documents, update/delete result objects, or aggregation results.

If Include Query Info is enabled, the output includes metadata about the executed query and options alongside the main data.

No binary data output is produced by this node.

Example output for Count operation (without query info):

{
  "json": 42
}

With query info enabled:

{
  "json": {
    "data": 42,
    "query": { "age": { "$gte": 18 } },
    "operation": "count",
    "options": {}
  }
}

Dependencies

  • Requires a MongoDB instance accessible via a connection string.
  • Uses Mongoose ODM library for schema-based interactions.
  • Requires an API key credential or similar authentication configured in n8n to connect to MongoDB.
  • Supports multi-database connections by selecting the database name dynamically.

Troubleshooting

  • Connection timeout or failure:
    Errors like "Connection timeout after 10 seconds", "MongoDB connection error", or "Database connection failed" indicate network issues or incorrect connection strings. Verify your MongoDB URI, ensure the server is running, and check firewall/network settings.

  • Authentication errors:
    Messages including "authentication failed" suggest invalid username/password or wrong authentication database. Double-check credentials and authSource parameters.

  • Host not found:
    Errors mentioning "ENOTFOUND" or "getaddrinfo" mean the hostname/IP is unreachable or incorrect. Confirm DNS resolution and network connectivity.

  • Limit exceeds maximum allowed:
    If the limit option is set above 10,000, the node throws an error to prevent excessive resource usage. Reduce the limit accordingly.

  • Invalid JSON in query or schema:
    The node parses JSON inputs for queries and schemas. Malformed JSON will cause parsing errors. Use valid JSON syntax.

  • Large result sets warning:
    When returning more than 5,000 documents, a console warning suggests adding indexes or refining filters to improve performance.

Links and References

Discussion