취미와 밥줄사이
[Python] MongoDB Sort 본문
결과 정렬
- sort() 메서드를 사용하여 결과를 오름차순 또는 내림차순으로 정렬합니다.
- sort() 메서드는 "fieldname"에 대해 하나의 매개변수를 사용하고 "direction"에 대해 하나의 매개변수를 사용합니다(오름차순이 기본 방향임).
# Sort the result alphabetically by name:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
mydoc = mycol.find().sort("name")
for x in mydoc:
print(x)
내림차순 정렬
- value -1을 두 번째 매개변수로 사용하여 내림차순으로 정렬합니다.
- sort("name", 1) #ascending
- sort("name", -1) #descending
Sort the result reverse alppabetically by name:
import pymongo
myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["mydatabase"]
mycol = mydb["customers"]
mydoc = mycol.find().sort("name", -1)
for x in mydoc:
print(x)
REFERENCE
https://www.w3schools.com/python/python_mongodb_sort.asp
Python MongoDB Sort
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
www.w3schools.com
'DB' 카테고리의 다른 글
[Python] MongoDB Delete Document (0) | 2021.11.03 |
---|---|
[SQLAlchemy] SQLAlchemy이란? (0) | 2021.11.01 |
[SQL] EXISTS 연산자 (0) | 2021.11.01 |
[SQL] HAVING 절 (0) | 2021.11.01 |
[SQL] GROUP BY 문 (0) | 2021.11.01 |