跳到正文
发现
其他开发者工具

DocumentDB:兼容 MongoDB 的文档数据库

基于 PostgreSQL 的开源 MongoDB 兼容文档数据库,适合想用文档模型又依赖 Postgres 的开发者。

admin
发布于 2026-10-28
浏览
4
访问
0
下载
0
版本
v1.0-0
该作品还没有经过官方审核。访问演示、下载或复制命令前,请自行确认来源可信。

详细介绍

项目简介

DocumentDB 是一个开源的、兼容 MongoDB 的文档数据库,构建在 PostgreSQL 之上。它在 PostgreSQL 框架内原生实现了面向文档的 NoSQL 能力,可以直接对 BSON(Binary JSON)数据类型进行增删改查,同时还能承担全文检索、地理空间查询和向量搜索等更复杂的工作负载。

项目采用 MIT 许可证,可自托管部署,让组织完全掌控自己的数据与基础设施,同时享受 PostgreSQL 长期积累的稳定性、性能、扩展性、索引与安全合规能力。

组成组件

  • pg_documentdb_core:PostgreSQL 扩展,为原生 Postgres 引入 BSON 数据类型及其操作。
  • pg_documentdb:DocumentDB 的公开 API 层,提供对存储中文档的 CRUD 能力。
  • pg_documentdb_gw:网关协议转换层,把用户使用的 MongoDB API 转换为 PostgreSQL 查询。

主要功能

  • 兼容 MongoDB 的文档数据库接口,可复用现有的 MongoDB 驱动与工具链。
  • 在 PostgreSQL 中原生支持 BSON 数据类型与相关操作。
  • 支持单条与批量插入、查询、聚合管道等文档操作。
  • 支持全文搜索、地理空间查询与向量搜索等进阶负载。
  • 完全开源(MIT),支持本地/自托管部署。
  • 官方提供安装脚本、系统软件包与容器镜像三种获取方式。

安装与使用

使用安装脚本

在干净的 Ubuntu 24.04 或兼容 RHEL 9 的主机(amd64 或 arm64)上,脚本会从 documentdb.io 软件源安装当前稳定版本:

curl -fsSLo documentdb-install.sh https://github.com/documentdb/documentdb/releases/download/v1.0-0/install.sh &&
sudo sh documentdb-install.sh

使用容器启动

# 删除本地可能已存在的 DocumentDB 镜像
docker image rm -f ghcr.io/documentdb/documentdb/documentdb-local:latest || echo "No existing documentdb image to remove"

# 拉取最新的 DocumentDB 镜像
docker pull ghcr.io/documentdb/documentdb/documentdb-local:latest

# 打个简短的标签
docker tag ghcr.io/documentdb/documentdb/documentdb-local:latest documentdb

# 使用自己设定的用户名和密码启动容器
docker run -dt -p 10260:10260 --name documentdb-container documentdb --username <YOUR_USERNAME> --password <YOUR_PASSWORD>

用 pymongo 连接并操作

import pymongo
from pymongo import MongoClient

client = pymongo.MongoClient(
    'mongodb://<YOUR_USERNAME>:<YOUR_PASSWORD>@localhost:10260/?tls=true&tlsAllowInvalidCertificates=true'
)

quickStartDatabase = client["quickStartDatabase"]
quickStartCollection = quickStartDatabase.create_collection("quickStartCollection")

quickStartCollection.insert_one({
    'name': 'John Doe',
    'email': '[email protected]',
    'address': '123 Main St, Anytown, USA',
    'phone': '555-1234'
})

for document in quickStartCollection.find():
    print(document)

pipeline = [
    {'$match': {'name': 'Alice Johnson'}},
    {'$project': {'_id': 0, 'name': 1, 'email': 1}}
]
for eachDocument in quickStartCollection.aggregate(pipeline):
    print(eachDocument)

注意事项

  • 创建容器时必须设置用户名与密码,否则认证无法工作。
  • 示例默认使用 10260 端口,以避免与本地其他数据库服务冲突;也可以改用 27017(MongoDB 标准端口)或其他可用端口,但要同步修改 docker run 命令和连接串中的端口号。
  • 连接串中的 tls=true&tlsAllowInvalidCertificates=true 与示例环境配套,正式环境请按实际证书情况调整。
  • 容器镜像默认会发送使用遥测数据,可通过 documentdb-local/PRIVACY.md 中的说明关闭。
  • 项目同时提供 PostgreSQL 15–18 的镜像标签 pg15-1.0.0 至 pg18-1.0.0,latest 对应 pg17-1.0.0。

本作品整理自 GitHub 开源项目 documentdb/documentdb,原作者 @documentdb,许可证 MIT。中文介绍由 AI 根据项目 README 整理,以原仓库为准。

更新记录

  1. v1.0-0当前版本

    首个正式 GA 版本:提供 PostgreSQL 17/18 的 Ubuntu 24.04 与 RHEL 9 软件包(amd64/arm64)、PG15–18 容器镜像,并附带一键安装脚本。

评论