用途:在 MongoDB(文档型)与 PostgreSQL(关系型)之间做选型,并完成两者的模式设计、查询编写、性能优化与日常运维。
主要能力:涵盖 CRUD 与查询操作符、MongoDB 聚合管道、SQL 查询(JOIN、子查询、CTE、窗口函数)、索引创建与优化、迁移、复制/分片/集群、备份与灾难恢复、用户与权限管理、慢查询分析,以及生产环境数据库管理。附带常见操作的 MongoDB 与 PostgreSQL 对照示例(插入、查询、更新、删除、建索引)。
选型建议:需要模式灵活、JSON/BSON 文档模型、水平分片、高写入吞吐或嵌套数据时选 MongoDB;需要强一致 ACID 事务、复杂关联、SQL 生态、严格约束或复杂分析查询时选 PostgreSQL。两者均支持 JSON 存储查询、全文检索、地理空间查询、复制与高可用。
使用方式:MongoDB 可用 Atlas 云(mongodb.com/atlas 注册建免费 M0 集群),连接串形如 mongodb+srv://user:pass@cluster.mongodb.net/db,shell 用 mongosh。PostgreSQL 在 Ubuntu/Debian 可用 sudo apt-get install postgresql postgresql-contrib 安装,sudo systemctl start postgresql 启动,psql -U postgres -d mydb 连接。参考文档分 mongodb-crud、mongodb-aggregation、mongodb-indexing、mongodb-atlas、postgresql-queries、postgresql-psql-cli、postgresql-performance、postgresql-administration。脚本目录 scripts/ 提供 db_migrate.py、db_backup.py、db_performance_check.py,例如 python scripts/db_migrate.py --db mongodb --generate "add_user_index"、python scripts/db_backup.py --db postgres --output /backups/、python scripts/db_performance_check.py --db mongodb --threshold 100ms。
适用场景:模式设计与建模、查询与聚合编写、索引与性能调优、迁移、备份恢复、生产运维。
限制:资料未给出具体版本兼容范围与各脚本的完整参数细节,使用前需自行核对官方文档;生产环境应启用认证与 TLS。