일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
31 |
- ethers websocket
- erc4337 contract
- Vue
- cloud hsm 사용하기
- Vue.js
- 계정추상화
- rust 기초
- 러스트 기초 학습
- ethers v6
- 스마트 컨트렉트 함수이름 중복
- redux toolkit 설명
- erc4337
- 체인의정석
- 러스트 기초
- 오블완
- 러스트기초
- SBT표준
- cloud hsm
- ethers typescript
- git rebase
- 스마트컨트렉트 예약어 함수이름 중복
- 스마트컨트렉트 함수이름 중복 호출
- 컨트렉트 동일한 함수이름 호출
- 머신러닝기초
- 티스토리챌린지
- ethers type
- cloud hsm 서명
- ambiguous function description
- redux 기초
- vue기초
- Today
- Total
체인의정석
Document DB 에서 upsert 사용하기 본문
AWS에서는 자체적으로 mongoDB가 아닌 DocumentDB를 사용한다.
이에 따라 로컬에서는 발생하지 않던 에러가 AWS배포 시에 발생하였다.
Document DB에서 UPsert를 사용하니 "retryWrites" 관련 오류가 발생하였다.
https://docs.aws.amazon.com/documentdb/latest/developerguide/mongo-apis.html
Supported MongoDB APIs, operations, and data types in Amazon DocumentDB - Amazon DocumentDB
Supported MongoDB APIs, operations, and data types in Amazon DocumentDB Amazon DocumentDB (with MongoDB compatibility) is a fast, scalable, highly-available, and fully managed document database service that supports MongoDB workloads. Amazon DocumentDB is
docs.aws.amazon.com
하지만 찾아보니 Upsert는 DocumentDB에서 존재하는 것을 볼 수 있었다.
더 찾아보니 Document DB에서 사용할 때는
SetRetryWrites(false)
이와 같이 db 커넥션 단계에서 설정 값을 넣어주어야 한다고 한다.
if r.client, err = mongo.Connect(context.Background(), options.Client().ApplyURI(cfg["datasource"].(string)).SetAuth(credential).SetRetryWrites(false)); err != nil {
해당 설정 값을 넣어주게 되면 다음과 같은 upsert 문법 사용이 가능하다.
func (q *QuestDB) UpsertTest(test entity.Test) error {
filter := bson.M{"id": info.ID}
update := bson.M{
"$set": bson.M{
"name": info.Name,
"description": info.Description,
"updated_at": time.Now(),
},
"$setOnInsert": bson.M{
"created_at": time.Now(),
},
}
opts := options.Update().SetUpsert(true)
_, err := q.collectionName.UpdateOne(context.TODO(), filter, update, opts)
return err
}
이런식으로 serOnInsert에 created_at 및 최초 insert 시 넣을 값을 쓰고 set일 때는 업데이트 할 값을 사용하면 된다.
SetUpsert(true)해당 부분도 위의 설정 값이 없다면 오류가 나지만 있다면 오류가 나지 않게 된다.
'개발 > database' 카테고리의 다른 글
Mongo DB에서의 not null 처리 (0) | 2025.06.11 |
---|---|
MySQL) You are using safe update mode 에러 (1) | 2024.11.14 |
Nest.js & Cassandra 로 백엔드 세팅해보기 (0) | 2023.11.14 |
카산드라 DB mac에서 사용해보기 (0) | 2023.11.13 |
DB에서 인덱싱하는 법과 Sequalize에서 테이블 정의 시 index 넣는 방법 (0) | 2023.09.15 |