Skip to main content
MongoDB 和 Mongoose 可以在 Bun 中无需额外配置即可工作。本指南假设你已安装 MongoDB 并在开发机器上将其作为后台进程或服务运行。详情请参见 MongoDB 安装指南
MongoDB 运行后,创建一个目录并使用 bun init 初始化。
terminal

然后将 Mongoose 添加为依赖。
terminal

schema.ts 中,声明并导出一个 Animal 模型。
schema.ts

index.ts 中,导入 Animal,连接到 MongoDB,并向数据库添加一些数据。
index.ts

使用 bun run 运行该文件。
terminal

在构建应用时,请参考官方 MongoDBMongoose 文档。