NoSQL/MongoDB

[MongoDB] 몽고디비 함수 sort(), limit(), skip()

씨네 2022. 4. 19. 10:21
728x90

Q) midterm의 kor 점수가 60 이상인 document만 출력

 
db.qclass.find({"midterm.kor": {$gte: 60}})

Q) math가 40보다 크고 60보다 작거나 같은 document만 출력

db.qclass.find({$and:[{math:{$gt:40}}, {math:{$lte:60}}]})

db.qclass.find().sort({name:-1})

sort함수를 이용하여 정렬할수도 있습니다.

name: -1은 이름에 따라 내림차순으로 정렬됩니다.

db.qclass.find({math:{$exists:true}},{_id:0,name:1,math:1}).sort({math:1}).limit(1)

math가 존재하는 데이터들의 name과 math값을 출력하고 math의 오름차순으로 정렬하며 1개만 출력합니다.

db.qclass.find({math:{$exists:true}},{_id:0,name:1,math:1}).sort({math:-1}).limit(1)

위의 명령과 같지만 내림차순으로 출력하여 가장 높은것이 출력되네요

db.qclass.find({},{_id:0, name:1}).skip(2)

skip함수는 ()안에 적힌 숫자만큼 생략합니다.

728x90