# nest-mongosse-transaction **Repository Path**: wx375149069_admin/nest-mongosse-transaction ## Basic Information - **Project Name**: nest-mongosse-transaction - **Description**: 此仓库是NestJs 使用 mongoose 操作Mongodb 数据库 和 事务 - **Primary Language**: NodeJS - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-02-10 - **Last Updated**: 2025-04-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README ## Description 此仓库是NestJs 使用 mongoose 操作Mongodb 数据库 和 事务 // 基础版 windows 本地软件下载 MongoDB : https://www.mongodb.com/try/download/community (会自动勾选下载MongoDB Compass, 最好下载,当然也可以用Navicat 去查看对应数据库) MongoDB Shell: https://www.mongodb.com/try/download/shell 事务 - 本地开发: **1、app.module文件** ``` import { Module } from '@nestjs/common'; import { AppController } from './app.controller'; import { AppService } from './app.service'; import { MongooseModule } from '@nestjs/mongoose'; import { DogModule } from './dog/dog.module'; @Module({ imports: [ MongooseModule.forRoot('mongodb://localhost:27017/dashan-test', { maxPoolSize: 1000, // 设置连接池大小为10 connectTimeoutMS: 10000, // 设置连接超时时间为5秒 socketTimeoutMS: 60000, // 设置套接字超时时间为45秒、 w: 'majority', // 重要:启用副本集事务支持 replicaSet: 'rs0' // 开发环境可用单节点副本集 }), DogModule ], controllers: [AppController], providers: [AppService], }) export class AppModule {} ``` **2、MongoDB.config 里加相关配置 replication: replSetName: rs0, 添加rs0事务副本** ``` # mongod.conf # for documentation of all options, see: # http://docs.mongodb.org/manual/reference/configuration-options/ # Where and how to store data. storage: dbPath: C:\Program Files\MongoDB\Server\8.0\data # where to write logging data. systemLog: destination: file logAppend: true path: C:\Program Files\MongoDB\Server\8.0\log\mongod.log # network interfaces net: port: 27017 bindIp: 127.0.0.1 #processManagement: #security: #operationProfiling: #replication: 重点加以下配置 replication: replSetName: rs0 #sharding: ## Enterprise-Only Options: #auditLog: ``` **3、管理员身份权限在mongodb 的目录下,例如:C:\Program Files\MongoDB\Server\8.0, 运行cmd,输入以下命令** `mongod --replSet rs0 --port 27017 --dbpath /data/db --bind_ip localhost` **4、在windows里重启MongoDB服务** **5、MongoDB Shell里 输入数据库名称并初始化, 例如dashan-nest数据库,调起事务副本** ![输入图片说明](1111.png) 输入下方命令初始化 ``` // 初始化 rs.initiate() // 查看状态 rs.status() ``` ## Installation ```bash $ npm install ``` ## Running the app ```bash # development $ npm run start # watch mode $ npm run start:dev # production mode $ npm run start:prod ``` ## Test ```bash # unit tests $ npm run test # e2e tests $ npm run test:e2e # test coverage $ npm run test:cov ``` ## Support Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please [read more here](https://docs.nestjs.com/support). ## License Nest is [MIT licensed](LICENSE).