[MongoDB] sort by date

Yuri Lee·2022년 5월 13일
0

Intro

MongoDB 에서 data 를 sort 하는 방법에 대해 알아보도록 하자.

How to do

user 라는 컬렉션이 있고, date 타입의 createdAt 라는 필드가 있다고 가정하자. (ex. "createdAt" : "2022-05-12T14:28:07.907+0000")

최근 createdAt 를 기준으로 조회하고 싶을 경우 아래의 shell을 실행시키면 된다.

db.getCollection("user").find().sort({"createdAt": -1}) // --- 1 for asc and -1 for desc

https://stackoverflow.com/questions/36441661/mongodb-sort-by-date

profile
Step by step goes a long way ✨

0개의 댓글