Skip to content

Commit 3ebf17f

Browse files
committed
[#147] Add limit skip method example and summary
1 parent 1c62dfd commit 3ebf17f

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed

Summary/ms/Chapter_12/12.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,6 +1545,63 @@ insertCsvToMongo(filename, "users", { birthday: -1, name: 1 });
15451545
15461546
### limit과 skip 메서드
15471547
1548+
10만 건이나 되는 많은 데이터를 `find({})` 형태로 찾는 것은 문제가 존재한다.<br/>
1549+
`find` 메서드는 이런 상황에 대응하도록 검색된 데이터의 개수를 제한하는 `limit` 메서드를 제공한다.<br/>
1550+
또한 페이징 등의 기능을 할 수 있도록 결과의 앞 N번째를 거를 수 있는 `skip` 메서드도 제공한다.<br/>
1551+
아래 코드는 검색된 결과 중에 100번째부터 다섯 개의 건수만 선택하는 예시다.<br/>
1552+
1553+
```typescript
1554+
let cursor = await usersCollection
1555+
.find({})
1556+
.sort({ birthday: -1, name: 1 })
1557+
.skip(100)
1558+
.limit(5);
1559+
```
1560+
1561+
아래와 같이 저장한 `users` 컬렉션에서도 동일하게 적용해 몽고DB에서 값을 가져올 수 있다.<br/>
1562+
1563+
- `findLimitSkip.ts`
1564+
1565+
```typescript
1566+
import { connect } from "./mongodb/connect";
1567+
import { IFake } from "./fake";
1568+
1569+
const findLimitSkip = async () => {
1570+
let connection;
1571+
1572+
try {
1573+
connection = await connect();
1574+
const db = await connection.db("ch12-2");
1575+
const usersCollection = db.collection("users");
1576+
1577+
let cursor = await usersCollection
1578+
.find({})
1579+
.sort({ birthday: -1, name: 1 })
1580+
.skip(100)
1581+
.limit(5);
1582+
let result = await cursor.toArray();
1583+
console.log(
1584+
result.map((user: IFake) => ({
1585+
name: user.name,
1586+
birthday: user.birthday,
1587+
}))
1588+
);
1589+
} catch (e) {
1590+
console.log(e.message);
1591+
} finally {
1592+
connection.close();
1593+
}
1594+
};
1595+
1596+
findLimitSkip();
1597+
```
1598+
1599+
- `findLimitSkip.ts` 실행 결과
1600+
1601+
<img src="./images/12.png" width="400" height="auto">
1602+
1603+
위와 같이 정상적으로 5건의 데이터의 `name` 속성값과 `birthday` 속성값을 확인할 수 있다.<br/>
1604+
15481605
[[🔝위로가기]](#12장-프로젝트-실습)&nbsp; / &nbsp;[[🔙뒤로가기]](https://github.com/alstn2468/DoIt_Typescript_Programming/blob/master/README.md)
15491606
15501607
## 12-3 익스프레스로 API 서버 만들기
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { connect } from "./mongodb/connect";
2+
import { IFake } from "./fake";
3+
4+
const findLimitSkip = async () => {
5+
let connection;
6+
7+
try {
8+
connection = await connect();
9+
const db = await connection.db("ch12-2");
10+
const usersCollection = db.collection("users");
11+
12+
let cursor = await usersCollection
13+
.find({})
14+
.sort({ birthday: -1, name: 1 })
15+
.skip(100)
16+
.limit(5);
17+
let result = await cursor.toArray();
18+
console.log(
19+
result.map((user: IFake) => ({
20+
name: user.name,
21+
birthday: user.birthday,
22+
}))
23+
);
24+
} catch (e) {
25+
console.log(e.message);
26+
} finally {
27+
connection.close();
28+
}
29+
};
30+
31+
findLimitSkip();
281 KB
Loading

0 commit comments

Comments
 (0)