mongo-create-document
Insert JSON documents into MongoDB collections by specifying database, collection, and document data for data storage operations.
Instructions
Create a new document in a MongoDB collection
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| database | Yes | Database name | |
| collection | Yes | Collection name | |
| document | Yes | Document to insert as JSON object |
Implementation Reference
- src/index.ts:110-128 (handler)Handler function that ensures MongoDB connection, retrieves the collection, inserts the document using insertOne, and returns a success message with the inserted ID or throws an error.async ({ database: dbName, collection: collectionName, document }) => { try { const db = await ensureConnection(dbName); const collection: Collection = db.collection(collectionName); const result = await collection.insertOne(document); return { content: [ { type: "text", text: `Document created successfully with ID: ${result.insertedId}`, }, ], }; } catch (error) { throw new Error(`Failed to create document: ${error instanceof Error ? error.message : 'Unknown error'}`); } }
- src/index.ts:106-108 (schema)Zod input schema defining the required parameters: database name, collection name, and document object.database: z.string().describe("Database name"), collection: z.string().describe("Collection name"), document: z.record(z.any()).describe("Document to insert as JSON object"),
- src/index.ts:102-129 (registration)Registration of the 'mongo-create-document' tool using server.tool(), including name, description, input schema, and handler function.server.tool( "mongo-create-document", "Create a new document in a MongoDB collection", { database: z.string().describe("Database name"), collection: z.string().describe("Collection name"), document: z.record(z.any()).describe("Document to insert as JSON object"), }, async ({ database: dbName, collection: collectionName, document }) => { try { const db = await ensureConnection(dbName); const collection: Collection = db.collection(collectionName); const result = await collection.insertOne(document); return { content: [ { type: "text", text: `Document created successfully with ID: ${result.insertedId}`, }, ], }; } catch (error) { throw new Error(`Failed to create document: ${error instanceof Error ? error.message : 'Unknown error'}`); } } );