gpt4 book ai didi

azure - Cosmos DB : Retryable writes are not supported. 请通过指定禁用可重试写入

转载 作者:行者123 更新时间:2023-12-02 05:58:12 27 4
gpt4 key购买 nike

关于这个问题有很多线程,但没有一个有适合我的解决方案。我正在使用 NestJS、TypeORM 和 Cosmos DB Mongo API。

我正在我的计算机上运行 NestJS 服务,访问 Azure 中的数据库。我可以很好地从集合中读取数据,但是每当我尝试写入时,都会收到以下错误:

MongoError: Retryable writes are not supported. Please disable retryable writes by specifying "retrywrites=false" in the connection string or an equivalent driver specific config.

保存到数据库的逻辑的精简版本。我正在保存的对象在 Azure 的 Mongo Shell 中执行 db.collection.save() 时工作​​正常。但完全相同的 save() 在这里失败了

@Injectable()
export class PatientService {

constructor(
@InjectRepository(PatientsByUser)
private readonly patientsByUserRepo: MongoRepository<PatientsByUser>
) {}

async addPatientToFavoritesList() {
this.patientsByUserRepo.save({
email: "test",
patientList: [{
accountNumber: 1,
firstName: "test",
lastName: "test",
dob: "test"
}]
})
}

}

这是配置 TypeORM 的模块:

import {HttpModule, Module} from '@nestjs/common';
import {ConfigModule} from "@nestjs/config";
import {TypeOrmModule} from "@nestjs/typeorm";
import {PatientService} from "./patient.service";
import {PatientController} from "./patient.controller";
import {PatientsByUser} from "./patient.entity";

@Module({
imports: [
HttpModule,
ConfigModule.forRoot(),
TypeOrmModule.forRoot({
type: 'mongodb',
url: process.env.MONGODB_CONNECTION_STRING,
database: process.env.MONGODB_DATABASE,
entities: [
__dirname + '/**/*.entity{.ts,.js}',
],
ssl: true,
useUnifiedTopology: true,
useNewUrlParser: true,

}),
TypeOrmModule.forFeature([PatientsByUser])
],
providers: [PatientService],
controllers: [PatientController],
exports: [PatientService]
})
export class PatientModule {}

我的连接字符串确实包含retrywrites=false。我还尝试了 retryWrites=false,并在连接字符串的末尾尝试了两者。都不起作用。

MONGODB_CONNECTION_STRING=mongodb://portal-db-dev:<private><a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="a29f9fe2d2cdd0d6c3ce8fc6c08fc6c7d48ccfcdccc5cd8cc1cdd1cfcdd18cc3d8d7d0c78cc1cdcf" rel="noreferrer noopener nofollow">[email protected]</a>:10255/?ssl=true&replicaSet=globaldb&retrywrites=false&maxIdleTimeMS=120000&appName=@portal-db-dev@

我们的 Cosmos DB 是版本 4。我知道回滚到 3.2 可以解决此问题,但如果这是解决此问题的唯一方法,那将是次优的,并且对 Microsoft 而言是完全荒谬的。

package.json

{
"name": "portal-be",
"version": "0.0.1",
"description": "",
"author": "",
"private": true,
"license": "UNLICENSED",
"scripts": {
"prebuild": "rimraf dist",
"build": "nest build && mkdir dist/src/assets && cp -r src/assets/* dist/src/assets",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^7.6.13",
"@nestjs/config": "^0.6.3",
"@nestjs/core": "^7.6.13",
"@nestjs/passport": "^7.1.5",
"@nestjs/platform-express": "^7.6.13",
"@nestjs/swagger": "^4.7.15",
"@nestjs/typeorm": "^7.1.5",
"@types/mongodb": "^3",
"@types/passport-azure-ad": "^4.0.8",
"class-transformer": "^0.4.0",
"class-validator": "^0.13.1",
"dotenv": "^8.2.0",
"jwt-decode": "^3.1.2",
"moment": "^2.29.1",
"mongodb": "^3.6.9",
"passport": "^0.4.1",
"passport-azure-ad": "^4.3.0",
"reflect-metadata": "^0.1.13",
"rimraf": "^3.0.2",
"rxjs": "^6.6.7",
"soap": "^0.39.0",
"typeorm": "^0.2.32"
},
"devDependencies": {
"@nestjs/cli": "^7.5.6",
"@nestjs/schematics": "^7.2.7",
"@nestjs/testing": "^7.6.13",
"@types/express": "^4.17.11",
"@types/jest": "^26.0.20",
"@types/mongodb": "^3",
"@types/node": "^14.14.31",
"@types/supertest": "^2.0.10",
"@typescript-eslint/eslint-plugin": "^4.15.2",
"@typescript-eslint/parser": "^4.15.2",
"eslint": "^7.20.0",
"eslint-config-prettier": "^8.1.0",
"eslint-plugin-prettier": "^3.3.1",
"jest": "^26.6.3",
"prettier": "^2.2.1",
"supertest": "^6.1.3",
"ts-jest": "^26.5.2",
"ts-loader": "^8.0.17",
"ts-node": "^9.1.1",
"tsconfig-paths": "^3.9.0",
"typescript": "^4.1.5"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}

最佳答案

Cosmos DB 不支持可重试写入。

默认情况下,mongo shell 不启用可重试写入。

与 MongoDB 4.2 或更高版本兼容的 MongoDB 驱动程序默认启用可重试写入。

MongoDB Node.JS 驱动程序版本 3.3+ 与 MongoDB 4.2 兼容

要使用默认启用可重试写入的驱动程序连接到不支持可重试写入的数据库服务器,您需要通过在连接字符串 URI 中传递 retryWrites=false 来禁用可重试写入。

https://docs.mongodb.com/manual/core/retryable-writes/#enabling-retryable-writes

关于azure - Cosmos DB : Retryable writes are not supported. 请通过指定禁用可重试写入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68201298/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com