# mongoose 트랜잭션 ###### tags: `tech sharing` ### 트랜잭션 수행 방법 ``` import {startSession} from 'mongoose'; const session = await startSession(); try{ const a = userModel.create([{id:'123',name:'test'}],{session}); await session.commitTransaction(); } catch{ await session.abortTransaction(); } finally{ session.endSession(); } ``` ### 주의사항 - create 메쏘드를 사용할 때, 첫번째 인자를 배열로 넣어야 한다 ``` await userModel.create([{ id:'test',name: 'test123' }], { session }); ``` ### 현재 프로젝트에 적용한 곳 - 현재 프로젝트에서 addRatings와 confirmPurchase 두군데 트랜잭션 적용 - 이미지는 어떻게 할지 고민중.. [mongoose 공식 문서](https://mongoosejs.com/docs/api.html#model_Model.update)