@@ -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장-프로젝트-실습) / [[🔙뒤로가기]](https://github.com/alstn2468/DoIt_Typescript_Programming/blob/master/README.md)
15491606
15501607# # 12-3 익스프레스로 API 서버 만들기
0 commit comments