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 a variety of operations such as creating, finding, updating, deleting documents, counting documents, and running aggregation pipelines on specified collections within a MongoDB database.

Typical use cases include:

  • Deleting a single document from a collection based on a query filter.
  • Managing data in MongoDB collections without writing custom code by leveraging Mongoose schemas.
  • Running complex queries or aggregations to retrieve or manipulate data efficiently.

For example, the "Delete One" operation can be used to remove a specific user record from a "users" collection by specifying a query that matches the user's unique identifier.

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 within the database to operate on (e.g., "users").
Query A MongoDB query object in JSON format to filter documents for the operation. For "Delete One", it specifies which document to delete.
Schema Definition Defines the Mongoose schema for the collection in JSON format, specifying field names and types (e.g., "name": "String"). This is required to map the collection structure.
Include Query Info Boolean flag indicating whether to include raw MongoDB query information in the response. Useful for debugging or logging.
Options Additional options for the operation, including:
- Limit Maximum number of results to return (not applicable for "Delete One").
- Skip Number of documents to skip (not applicable for "Delete One").
- Sort Sort order for results (not applicable for "Delete One").
- Select Fields Fields to include or exclude in results (not applicable for "Delete One").
- Upsert Whether to create a document if it doesn't exist (only relevant for update operations).
- Multi Whether to update multiple documents (only relevant for update operations).
- Query Timeout (Ms) Maximum time in milliseconds to allow the query to run (1-300 seconds).
- Debug Mode Enables Mongoose debug mode to log all MongoDB queries to the console.

Output

The output is an array of JSON objects representing the result of the operation for each input item.

For the "Delete One" operation, the output JSON includes:

  • The result of the deletion operation, typically containing metadata such as the number of documents deleted.
  • Optionally, if "Include Query Info" is enabled, the raw MongoDB query used for deletion is included in the output under a structured format.

Example output snippet for a successful delete one operation:

{
  "deletedCount": 1,
  "query": { /* the query object used */ },
  "operation": "deleteOne"
}

No binary data output is produced by this node.

Dependencies

  • Requires a valid connection to a MongoDB instance accessible via a connection string.
  • Uses Mongoose ODM library to interact with MongoDB.
  • Requires an API key credential or authentication token configured in n8n to connect securely to the MongoDB database.
  • The node expects the user to provide a valid Mongoose schema definition matching the target collection's structure.

Troubleshooting

Common Issues

  • Connection errors: If the node cannot connect to MongoDB, verify the connection string, network accessibility, and that MongoDB is running.
  • Authentication failures: Check username, password, and authentication source settings in the credentials.
  • Invalid JSON in query or schema: Ensure that the JSON provided for queries and schema definitions is correctly formatted.
  • Limit exceeded: Although not applicable for "Delete One," other operations enforce a maximum limit to prevent large unintended data retrievals.
  • Timeouts: Queries taking longer than the specified timeout will fail; adjust the "Query Timeout" option accordingly.

Error Messages and Resolutions

  • MongoDB connection error: Indicates issues connecting to the database. Verify connection details and server status.
  • MongoDB authentication failed: Credentials are incorrect or insufficient permissions. Recheck credentials.
  • MongoDB host not found: DNS or network issue resolving the MongoDB host. Confirm hostname/IP and network connectivity.
  • MongoDB connection refused: MongoDB server is not accepting connections on the specified port. Ensure MongoDB is running and accessible.
  • Invalid JSON format: The JSON input for queries or schema is malformed. Validate JSON syntax before input.

Links and References

Discussion