Mongoose 필드(field)명 변경, Object 키 값 변경
2021. 4. 22. 19:33ㆍMongoDB, Mongoose
const January = new Schema({
rankPoint: { type:Number },
lastWeekPoint: { type:Number },
totalScore: { type:Number },
rank: { type:Number },
});
"January":{
"rankPoint" : 0,
"lastWeekPoint" : 0,
"totalScore" : 0,
"rank" : 3
}
January 스키마가 구성되어 있다.
lastWeekPoint를 lastDayPoint로 바꾸려고 할때
$rename을 사용한다.
사용방법
January.findOneAndUpdate({},{$rename: {lastWeekPoint: 'lastDayPoint' }}, {multi: true})
명령어를 넣어주면 value값은 변경되지않고 key값만 깔끔하게 변경이 된다.
한개의 document만 변경한 예시이고
복수의 document를 변경할때는 updateMany() 같은 함수를 사용하면 된다.
출처 : stackoverflow.com/questions/53447156/rename-field-in-mongoose
728x90
반응형
'MongoDB, Mongoose' 카테고리의 다른 글
Mongoose 다중 connect 연결 방법 (0) | 2022.02.11 |
---|---|
mongoose options usecreateindex,usefindandmodify are not supported 에러 해결 방법 (0) | 2021.11.04 |
Mongoose 페이징 처리 (Node) (0) | 2021.02.24 |
Cosmos DB에 mongoose sort기능 사용시 오류 The index path corresponding to the specified order-by item is excluded (0) | 2020.10.08 |
Moogoose "__v" 필드 (0) | 2020.06.10 |