본문 바로가기
프로그래밍 개발/DB

MongoDB - Document Query(조회) 2

by Jinseok Kim 2021. 1. 18.
반응형

 

 

Document Query(조회) 2

 

 

 

 find() 메소드 활용  sort(), limit(), skip()

 

orders 라는 데이터 추가

db.orders.insert[
...     { "_id": 1, "item": { "category": "cake", "type": "chiffon" }, "amount": 10 },
...     { "_id": 2, "item": { "category": "cookies", "type": "chocolate chip" }, "amount": 50 },
...     { "_id": 3, "item": { "category": "cookies", "type": "chocolate chip" }, "amount": 15 },
...     { "_id": 4, "item": { "category": "cake", "type": "lemon" }, "amount": 30 },
...     { "_id": 5, "item": { "category": "cake", "type": "carrot" }, "amount": 20 },
...     { "_id": 6, "item": { "category": "brownies", "type": "blondie" }, "amount": 10 }
... ]

 

 

 

cursor.sort( DOCUMENT )

이 메소드는 데이터를 정렬할 때 사용된다. 매개변수로는 어떤 KEY 를 사용하여 정렬 할 지 알려주는 document 를 전달한다.

 

 

{ KEY: value }
  • KEY 는 데이터의 field 이름이고, value 의 값은 1 혹은 -1 이고 이 값을 1로 설정하면 오름차순으로, -1로 하면 내림차순으로 정렬한다.
  • 또한 여러 KEY를 입력 할 수 있고 먼저 입력한 KEY가 우선권을 갖는다.

 

 

> db.orders.find().sort( { "_id": 1 } )

 _id 의 값을 사용하여 오름차순으로 정렬하기

> db.orders.find().sort( { "_id": 1 } )
{ "_id" : 1, "item" : { "category" : "cake", "type" : "chiffon" }, "amount" : 10 }
{ "_id" : 2, "item" : { "category" : "cookies", "type" : "chocolate chip" }, "amount" : 50 }
{ "_id" : 3, "item" : { "category" : "cookies", "type" : "chocolate chip" }, "amount" : 15 }
{ "_id" : 4, "item" : { "category" : "cake", "type" : "lemon" }, "amount" : 30 }
{ "_id" : 5, "item" : { "category" : "cake", "type" : "carrot" }, "amount" : 20 }
{ "_id" : 6, "item" : { "category" : "brownies", "type" : "blondie" }, "amount" : 10 }

 

 

 

 

 

 

> db.orders.find().sort( { "amount": 1, "_id": -1 } )

 amount 값을 사용하여 오름차순으로 정렬하고, 정렬한 값에서 id 값은 내림차순으로 정렬하기

> db.orders.find().sort( { "amount": 1, "_id": -1 } )
{ "_id" : 6, "item" : { "category" : "brownies", "type" : "blondie" }, "amount" : 10 }
{ "_id" : 1, "item" : { "category" : "cake", "type" : "chiffon" }, "amount" : 10 }
{ "_id" : 3, "item" : { "category" : "cookies", "type" : "chocolate chip" }, "amount" : 15 }
{ "_id" : 5, "item" : { "category" : "cake", "type" : "carrot" }, "amount" : 20 }
{ "_id" : 4, "item" : { "category" : "cake", "type" : "lemon" }, "amount" : 30 }
{ "_id" : 2, "item" : { "category" : "cookies", "type" : "chocolate chip" }, "amount" : 50 }

 

 

 

 

 

 

cursor.limit( value )

이 메소드는 출력할 데이터 갯수를 제한할 때 사용된다. value 파라미터는 출력 할 갯수 값이다.

 

 

> db.orders.find().limit(3)

출력 할 갯수를 3개로 제한하기

> db.orders.find().limit(3)
{ "_id" : 1, "item" : { "category" : "cake", "type" : "chiffon" }, "amount" : 10 }
{ "_id" : 2, "item" : { "category" : "cookies", "type" : "chocolate chip" }, "amount" : 50 }
{ "_id" : 3, "item" : { "category" : "cookies", "type" : "chocolate chip" }, "amount" : 15 }

 

 

 

 

cursor.skip( value )

이 메소드는 출력 할 데이터의 시작부분을 설정할 때 사용된다.  value 값 갯수의 데이터를 생략하고 그 다음부터 출력한다.

 

 

> db.orders.find().skip(2)

2개의 데이터를 생략하고 그 다음부터 출력

> db.orders.find().skip(2)
{ "_id" : 3, "item" : { "category" : "cookies", "type" : "chocolate chip" }, "amount" : 15 }
{ "_id" : 4, "item" : { "category" : "cake", "type" : "lemon" }, "amount" : 30 }
{ "_id" : 5, "item" : { "category" : "cake", "type" : "carrot" }, "amount" : 20 }
{ "_id" : 6, "item" : { "category" : "brownies", "type" : "blondie" }, "amount" : 10 }

 

 

 

응용

order 를 최신순으로 한 페이지당 2개씩 나타낸다.

> var showPage = function(page){
... return db.orders.find().sort( { "_id": -1 } ).skip((page-1)*2).limit(2);
... }
> showPage(1)
{ "_id" : 6, "item" : { "category" : "brownies", "type" : "blondie" }, "amount" : 10 }
{ "_id" : 5, "item" : { "category" : "cake", "type" : "carrot" }, "amount" : 20 }
> showPage(2)
{ "_id" : 4, "item" : { "category" : "cake", "type" : "lemon" }, "amount" : 30 }
{ "_id" : 3, "item" : { "category" : "cookies", "type" : "chocolate chip" }, "amount" : 15 }
> showPage(3)
{ "_id" : 2, "item" : { "category" : "cookies", "type" : "chocolate chip" }, "amount" : 50 }
{ "_id" : 1, "item" : { "category" : "cake", "type" : "chiffon" }, "amount" : 10 }

''

반응형

'프로그래밍 개발 > DB' 카테고리의 다른 글

MongoDB - Index  (0) 2021.01.20
MongoDB - Document 수정  (0) 2021.01.19
MongoDB - Document Query(조회)  (0) 2021.01.18
MongoDB - 생성 및 제거하기  (0) 2021.01.15
MongoDB - 기본 설치 및 개념  (0) 2021.01.14

댓글