Skip to content

MongoDB Connector

The MongoDB connector provides full access to MongoDB databases with 10 live tools spanning collection management, aggregation pipelines, index operations, and document CRUD.

Authentication

Provide a MongoDB connection string:

json
{
  "name": "Production MongoDB",
  "source_type": "mongodb",
  "config": {
    "connection_string": "mongodb+srv://agentcy:password@cluster0.abc123.mongodb.net/myapp",
    "database": "myapp",
    "read_only": true
  }
}

MongoDB Atlas

For Atlas clusters, use the mongodb+srv:// connection string from your cluster's connection dialog:

json
{
  "config": {
    "connection_string": "mongodb+srv://agentcy:password@cluster0.abc123.mongodb.net/?retryWrites=true&w=majority",
    "database": "myapp"
  }
}

Self-Hosted (Replica Set)

json
{
  "config": {
    "connection_string": "mongodb://agentcy:password@mongo1:27017,mongo2:27017,mongo3:27017/myapp?replicaSet=rs0&authSource=admin",
    "database": "myapp"
  }
}

TIP

Create a dedicated read-only user for Agentcy. In MongoDB, grant the read role on the target database.

Creating a Read-Only User

javascript
use admin
db.createUser({
  user: "agentcy",
  pwd: "secure-password",
  roles: [
    { role: "read", db: "myapp" }
  ]
})

Live Tools (10)

ToolDescriptionArguments
mongodb_list_collectionsList all collections with document counts and sizesdatabase?
mongodb_find_oneGet a single document matching a filter with collection statscollection, filter?
mongodb_find_documentsQuery documents with filter, projection, sort, and limitcollection, filter?, projection?, sort?, limit?
mongodb_aggregateRun an aggregation pipelinecollection, pipeline
mongodb_get_collection_statsGet collection statistics including document count and sizecollection
mongodb_list_indexesList indexes on a collectioncollection
mongodb_insert_documentInsert a single documentcollection, document
mongodb_insert_manyInsert multiple documentscollection, documents
mongodb_update_documentsUpdate documents matching a filtercollection, filter, update, multi?
mongodb_delete_documentsDelete documents matching a filtercollection, filter, multi?

WARNING

The mongodb_insert_document, mongodb_insert_many, mongodb_update_documents, and mongodb_delete_documents tools are disabled when read_only is true (the default). Enable write access only when explicitly needed, and always with the approval flow enabled.

Example: Agent Using MongoDB Tools

User: "What collections are in the myapp database and what does the users collection look like?"

Agent: Let me explore the database.
→ Calls mongodb_list_collections()
→ Returns: users (145K docs, 89 MB), orders (2.1M docs, 1.2 GB),
   products (12K docs, 4 MB), sessions (890K docs, 320 MB)
→ Calls mongodb_find_one(collection: "users")
→ Reports inferred schema with field types, null rates, and sample documents

ETL Ingestion

Nodes Created

Node LabelPropertiesSource
Databasename, type ("mongodb"), host, collections_countConnection
Collectionname, database, document_count, avg_doc_size, total_size, cappedCollection stats
Indexname, collection, keys, unique, sparse, ttlIndex listing
MongoFieldname, path, bson_type, null_percentage, distinct_countSchema inference

Relationships Created

RelationshipFromTo
CONTAINSDatabaseCollection
HAS_INDEXCollectionIndex
HAS_FIELDCollectionMongoField
REFERENCESCollectionCollection (via DBRef or naming convention)

Graph Queries

cypher
-- Find collections without indexes (besides _id)
MATCH (c:Collection)
WHERE NOT (c)-[:HAS_INDEX]->(:Index {name: NOT "_id_"})
AND c.document_count > 10000
RETURN c.name, c.document_count

-- Map inter-collection references
MATCH (c1:Collection)-[:REFERENCES]->(c2:Collection)
RETURN c1.name, c2.name

-- Find the largest collections
MATCH (c:Collection)
RETURN c.name, c.document_count, c.total_size
ORDER BY c.total_size DESC
LIMIT 10

Configuration Reference

FieldTypeDefaultDescription
connection_stringstringrequiredMongoDB connection URI
databasestringrequiredDatabase to connect to
read_onlybooltrueDisable write tools (insert, update, delete)
max_query_docsint1000Maximum documents returned by mongodb_find_documents
query_timeout_secsint30Query execution timeout
schema_sample_sizeint100Documents to sample for schema inference
sync_indexesbooltrueInclude indexes in ingestion
sync_schemabooltruePerform schema inference during ingestion
exclude_collectionsstring[][]Collections to exclude from ingestion
tlsboolautoEnable TLS (auto-detected from connection string)
tls_ca_certstring--CA certificate for TLS connections

Aggregation Pipeline Examples

The mongo_aggregate tool supports full MongoDB aggregation pipelines:

json
// Count orders by status
{
  "collection": "orders",
  "pipeline": [
    { "$group": { "_id": "$status", "count": { "$sum": 1 } } },
    { "$sort": { "count": -1 } }
  ]
}

// Find top customers by total spend
{
  "collection": "orders",
  "pipeline": [
    { "$match": { "created_at": { "$gte": "2025-01-01" } } },
    { "$group": {
      "_id": "$customer_id",
      "total_spent": { "$sum": "$total" },
      "order_count": { "$sum": 1 }
    }},
    { "$sort": { "total_spent": -1 } },
    { "$limit": 10 }
  ]
}

Troubleshooting

ErrorCauseFix
Authentication failedInvalid credentials or auth sourceVerify username/password and authSource parameter
Connection timeoutServer unreachable or IP not allowlistedCheck network rules; for Atlas, add your IP to the allowlist
Not authorized on dbUser lacks permissions on the target databaseGrant the read role on the database
Topology closedConnection pool exhausted or server shutdownCheck server health and connection limits
BSONErrorInvalid query filter or pipeline syntaxVerify JSON syntax matches MongoDB query format

Built by AgentcyLabs. For in-house deployment or Agentcy Cloud (PaaS) access, visit agentcylabs.com.