개발 => 복습 후 재정리 대기/MongoDB

[MongoDB][Pymongo][Python] 정렬 - sort

장 상 현 2021. 5. 4.

 

sort('name', 1) #ascending
sort('name', -1) #descending



Example
Sort the result reverse alphabetically 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)

find().sort('name', 1) #ascending 순차정렬
find().sort('name', -1) #descending 역차정렬

 

Example

Sort the result reverse alphabetically 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)

댓글