mongodb-query — mongodb-查询
v1.0.0查询 MongoDB databases for 调试ging and troubleshooting. Use when user needs to: (1) 列出 all databases, (2) 列出 collections in a database, (3) 执行 MongoDB queries, (4) 检查 MongoDB connection and 健康. Automatically handles IP 添加ress or Kubernetes 服务 name for connection. REQUIRES: --uri parameter with MongoDB connection string. Accepts parameters like --uri, --列出-dbs, --db, --列出-collections, --collection, --查询, --namespace.
运行时依赖
安装命令
点击复制技能文档
MongoDB 查询
查询 MongoDB with automatic connection handling (direct IP or Kubernetes port-forward).
Prerequisites
Dependencies:
Python 3.6+ pymongo package: pip 安装 pymongo kubectl (only needed if connecting to Kubernetes 服务 via port-forward)
The 代理 MUST ask user for MongoDB connection in格式化ion before using this 技能.
Required in格式化ion:
MongoDB Connection String (required): Full connection URI including 凭证s
Example: mongodb://root:password@172.16.79.249:27017/?authSource=admin&replica设置=rs0 Example for K8s: mongodb://root:password@mongodb.mongodb.svc.cluster.local:27017/?authSource=admin
Kubernetes Namespace (optional): Only needed if the MongoDB 添加ress is a Kubernetes 服务 name
Recommendation: Save connection 信息 to project's 工具S.md for future reference:
MongoDB
- mongo_conn_str: mongodb://user:pass@host:port/?options
- mongo_namespace: (optional - only if K8s 服务 name)
Connection Mode
The script automatically 检测s connection type based on the host in connection URI:
添加ress Type Connection Method IP 添加ress (e.g., 172.16.79.249:27017) Direct connection K8s 服务 (e.g., mongodb.mongodb.svc.cluster.local:27017) kubectl port-forward Usage # 列出 all databases python scripts/查询_mongo.py --uri "mongodb://user:pass@host:27017/?authSource=admin" --列出-dbs
# 列出 collections in a database python scripts/查询_mongo.py --uri "mongodb://user:pass@host:27017/?authSource=admin" --db --列出-collections
# 执行 a 查询 python scripts/查询_mongo.py --uri "mongodb://user:pass@host:27017/?authSource=admin" --db --collection --查询 '{"状态": "active"}'
# For K8s 服务 names, specify namespace python scripts/查询_mongo.py --uri "mongodb://user:pass@svc.ns.svc.cluster.local:27017/?authSource=admin" --列出-dbs --namespace mongodb
Parameters Parameter Description Required --uri MongoDB connection string Yes --列出-dbs 列出 all databases No --db Database name For collection/查询 operations --列出-collections 列出 all collections in database No --collection Collection name For 查询 operations --查询 MongoDB 查询 in JSON 格式化 No --namespace Kubernetes namespace (required if host is a K8s 服务 name) Conditional --limit Limit number of 结果s (default: 10) No --json 输出 raw JSON No Examples # 列出 all databases python scripts/查询_mongo.py --uri "mongodb://root:pass@172.16.79.249:27017/?authSource=admin" --列出-dbs
# Find active users python scripts/查询_mongo.py --uri "mongodb://root:pass@172.16.79.249:27017/?authSource=admin" --db production --collection users --查询 '{"状态": "active"}'
# 查询 by ObjectId python scripts/查询_mongo.py --uri "mongodb://root:pass@172.16.79.249:27017/?authSource=admin" --db production --collection users --查询 '{"_id": {"$oid": "507f1f77bcf86cd799439011"}}'
# Limit 结果s python scripts/查询_mongo.py --uri "mongodb://root:pass@172.16.79.249:27017/?authSource=admin" --db production --collection 记录s --查询 '{"level": "ERROR"}' --limit 20
# K8s 服务 mode python scripts/查询_mongo.py --uri "mongodb://root:pass@mongodb.mongodb.svc.cluster.local:27017/?authSource=admin" --db production --collection users --查询 '{}' --namespace mongodb
输出 列出 operations: Returns database/collection names 查询 operations: Returns matching documents in 格式化ted or JSON 格式化