codestates / codestates/Lollin-server
[error handling] mongo db sort
- Dominant language
- No language data
- Stars
- 0
- Forks
- 0
- PR merge metrics
- No merged PRs in 30d
Description
**어떤 에러인가요?**
* 몽고디비에서 객체의 특정 속성을 기준으로 정렬하는데 문제가 발생했다. 분명 db.collection.find().sort(key:-1) 을 하면 객체중 key 값이 가장 큰 요소가 맨처음 와야 하는데, 그러지 않았다.
**에러 핸들링 방법**
* 알고보니 key 값이 문자열이여서 sort 함수가 제대로 작동을 하지 않았던 것이다. 따라서 일단 객체를 모두 받아와 변수에 저장한다음, 반복문을 돌려 key값을 숫자로 바꾸어 주고 다시 정렬해서 해결했다.
```javascript
let champIdArr = [];
for (let champ of result) {
champIdArr.push(Number(champ.key));
}
champIdArr.sort((a, b) => b - a);
let recentChampId = champIdArr[0].toString();
select('champions', { key: recentChampId }, (err, result2) => {
if (err) {
console.log(err);
res.send(err);
} else {
console.log(result2[0]);
let name = result2[0].id;
let key = result2[0].key;
let final = {};
final[name] = key;
res.send(final);
}
});```
Contributor guide
No contributing guide indexed for this repository
Research direction
Start by reproducing the MongoDB sort described in the issue, comparing string and numeric values for key. Review the shown JavaScript workaround and the surrounding selection flow; done means the intended highest-key item is selected without relying on an incorrect string sort.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, mongodb
- Domain
- databases
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100