Kingfoodmart Mongoose icon

Kingfoodmart 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 various database operations such as creating, finding, updating, deleting documents, running aggregation pipelines, and counting documents within a specified collection.

The "Create" operation specifically enables the creation of new documents in a MongoDB collection by defining a Mongoose schema and providing the document data to insert. This is useful when you want to programmatically add new records to your MongoDB collections with schema validation.

Practical examples:

  • Adding a new user profile to a "users" collection with fields like name, email, age, and registration date.
  • Inserting a new product entry into an inventory collection with details such as product name, price, and stock quantity.

Properties

Name Meaning
Database The name of the MongoDB database to connect to. Leave empty to use the default from credentials.
Collection The name of the MongoDB collection where the document will be created.
Schema Definition JSON object defining the Mongoose schema for the collection. Specifies field names and types.
Document JSON object representing the document data to create in the collection.

Output

The output is an array of items, each containing a json property with the result of the create operation. For the "Create" operation, this includes the newly created document as stored in MongoDB, converted to a plain JSON object.

Example output structure:

{
  "json": {
    "_id": "60d5ec49f8d2e30a3c4b7a1e",
    "name": "John Doe",
    "email": "john.doe@example.com",
    "age": 30,
    "created_at": "2024-06-01T12:00:00.000Z"
  }
}

No binary data output is produced by this operation.

Dependencies

  • Requires a MongoDB instance accessible via a connection string.
  • Uses Mongoose ODM for schema definition and database interaction.
  • Requires an API key credential or similar authentication token configured in n8n to connect securely to MongoDB.
  • Supports multi-database connections based on provided credentials and optional database name override.

Troubleshooting

  • Connection timeout or failure: If the node throws errors related to connection timeouts or inability to connect, verify that the MongoDB server is running, accessible from the network, and the connection string is correct.
  • Authentication failed: Errors mentioning authentication failure indicate incorrect username, password, or authentication database settings. Double-check credentials.
  • Invalid JSON format: The schema definition and document inputs must be valid JSON. Invalid JSON will cause parsing errors.
  • Schema mismatch: Ensure the schema definition matches the expected document structure; otherwise, Mongoose may reject the document or behave unexpectedly.
  • Large result sets warning: Although not applicable to "Create," other operations warn if large datasets are returned without filters or limits.
  • Continue on Fail: If enabled, the node will continue processing subsequent items even if one fails, returning error details in the output.

Links and References

Discussion