MongoDB Mongoose icon

MongoDB 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 a variety of operations such as finding documents, creating new entries, updating existing ones, deleting documents, counting records, and running aggregation pipelines. The node is designed to work with multiple databases by allowing dynamic database selection.

Common scenarios where this node is beneficial include:

  • Querying user data or other collections with complex filters.
  • Updating or deleting specific documents based on criteria.
  • Aggregating data for reports or analytics.
  • Counting documents matching certain conditions.
  • Creating new documents in a collection.

For example, you can use the "Find" operation to retrieve all users older than 18, selecting only their names and emails. Or use the "Update" operation to modify multiple documents that match a query, optionally creating them if they don't exist.

Properties

Name Meaning
Database Name of the MongoDB database to connect to. Leave empty to use the default database from the configured credential.
Collection Name of the collection within the database to operate on.
Query MongoDB query object in JSON format to filter documents. Leave empty ({}) for no filtering (matches all).
Schema Definition JSON definition of the Mongoose schema for the target collection. Defines field types like String, Number, Date, etc.
Select Fields JSON string specifying which fields to include or exclude in the result (e.g., {"_id": 1, "name": 1}). Leave empty to select all fields. Only applicable for "find" and "findOne" operations.
Include Query Info Boolean flag indicating whether to include raw MongoDB query information in the output for debugging or logging purposes.
Options A collection of additional options:
- Limit Maximum number of results to return (default 1000, max 10,000).
- Skip Number of documents to skip before returning results (not applicable for aggregate operations).
- Sort JSON object defining sort order (e.g., {"name": 1, "age": -1}), not applicable for aggregate operations.
- Select Space-separated string listing fields to include/exclude (e.g., "name email -_id"), not applicable for aggregate operations.
- Upsert For update operations only: whether to create a document if none matches the query.
- Multi For update operations only: whether to update multiple documents matching the query.
- Query Timeout (Ms) Maximum time in milliseconds to allow the query to run (range 1000 to 300000 ms).
- Debug Mode Enables Mongoose debug mode to log all MongoDB queries to the console.

Output

The node outputs an array of items, each containing a json property with the operation result. The structure depends on the operation:

  • Find / Find One: Returns the matched documents as plain JSON objects, respecting selected fields. If Include Query Info is enabled, the output includes metadata about the query and options used.
  • Create: Returns the created document as JSON.
  • Update / Update One: Returns the MongoDB update result object, including counts of matched and modified documents. Includes query info if enabled.
  • Delete / Delete One: Returns the deletion result object with counts of deleted documents. Includes query info if enabled.
  • Count: Returns the count number of documents matching the query. Includes query info if enabled.
  • Aggregate: Returns the aggregation pipeline results as an array of JSON objects. Includes pipeline info if enabled.

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 for schema modeling and database operations.
  • Needs credentials configured with appropriate access rights to the target MongoDB database(s).
  • Supports multi-database setups by dynamically selecting the database name.
  • Optional debug mode logs queries to the console for troubleshooting.

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 database settings.
  • Invalid JSON inputs: Query, schema definition, or other JSON parameters must be valid JSON strings or objects; otherwise, parsing errors occur.
  • Exceeding limits: The node enforces a maximum limit of 10,000 documents per query for safety. Requests exceeding this will throw an error.
  • Large result sets warning: When returning more than 5,000 documents, a console warning suggests adding indexes or refining filters for performance.

Error Messages and Resolutions

  • "MongoDB connection error": Indicates failure to connect. Verify connection details and MongoDB server status.
  • "MongoDB authentication failed": Credentials are incorrect or insufficient permissions. Double-check credentials.
  • "MongoDB host not found": DNS or IP address issues. Confirm hostname/IP and network connectivity.
  • "MongoDB connection refused": MongoDB server not accepting connections on specified port. Ensure MongoDB is running and firewall rules allow access.
  • "Invalid JSON format": Input JSON is malformed. Validate JSON syntax before input.
  • "Limit exceeds maximum allowed": Reduce the requested limit to 10,000 or less.

Links and References

Discussion