Skip to main content
Glama

mongodb_insert_document

Insert JSON documents into MongoDB collections through the MCP Mac Apps Server. Specify database, collection, and document data to add records to your MongoDB database.

Instructions

Вставляет документ в коллекцию

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
databaseNameYesИмя базы данных
collectionNameYesИмя коллекции
documentYesJSON строка с документом для вставки

Implementation Reference

  • The primary handler function that executes the mongodb_insert_document tool. Connects to MongoDB via helper, parses JSON document, performs insertOne operation, and returns success message with inserted ID.
    private async mongodbInsertDocument( databaseName: string, collectionName: string, documentJson: string ) { const client = await this.getMongoClient(); try { const db = client.db(databaseName); const collection = db.collection(collectionName); const document = JSON.parse(documentJson); const result = await collection.insertOne(document); return { content: [ { type: "text", text: `Документ успешно вставлен в коллекцию "${collectionName}". ID: ${result.insertedId}`, }, ], }; } catch (error) { throw new Error( `Ошибка вставки документа: ${error instanceof Error ? error.message : String(error)}` ); } finally { await client.close(); } }
  • Input schema for the mongodb_insert_document tool defining required parameters: databaseName (string), collectionName (string), document (string JSON).
    name: "mongodb_insert_document", description: "Вставляет документ в коллекцию", inputSchema: { type: "object", properties: { databaseName: { type: "string", description: "Имя базы данных", }, collectionName: { type: "string", description: "Имя коллекции", }, document: { type: "string", description: "JSON строка с документом для вставки", }, }, required: ["databaseName", "collectionName", "document"], }, },
  • src/index.ts:359-364 (registration)
    Registration in the tool call dispatcher switch statement that routes calls to the mongodbInsertDocument handler.
    case "mongodb_insert_document": return await this.mongodbInsertDocument( args?.databaseName as string, args?.collectionName as string, args?.document as string );
  • Shared helper function to establish a MongoDB client connection, reused across all MongoDB-related tools.
    private async getMongoClient(): Promise<MongoClient> { const client = new MongoClient(MONGODB_URI); await client.connect(); return client; }
  • Python implementation of the handler for mongodb_insert_document tool (note: dispatch is commented out in handle_request).
    def mongodb_insert_document( database_name: str, collection_name: str, document_json: str ) -> str: """Inserts document into collection""" client = MongoClient(MONGODB_URI) try: db = client[database_name] collection = db[collection_name] document = json.loads(document_json) result = collection.insert_one(document) return f'Document successfully inserted into collection "{collection_name}". ID: {result.inserted_id}' except Exception as e: raise Exception(f"Error inserting document: {str(e)}") finally: client.close()

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/TrueOleg/MCP-expirements'

If you have feedback or need assistance with the MCP directory API, please join our Discord server