- Python 数据访问教程
- Python 数据访问 - 首页
- Python MySQL
- Python MySQL - 简介
- Python MySQL - 数据库连接
- Python MySQL - 创建数据库
- Python MySQL - 创建表
- Python MySQL - 插入数据
- Python MySQL - 查询数据
- Python MySQL - WHERE 子句
- Python MySQL - ORDER BY
- Python MySQL - 更新表
- Python MySQL - 删除数据
- Python MySQL - 删除表
- Python MySQL - LIMIT
- Python MySQL - JOIN
- Python MySQL - 游标对象
- Python PostgreSQL
- Python PostgreSQL - 简介
- Python PostgreSQL - 数据库连接
- Python PostgreSQL - 创建数据库
- Python PostgreSQL - 创建表
- Python PostgreSQL - 插入数据
- Python PostgreSQL - 查询数据
- Python PostgreSQL - WHERE 子句
- Python PostgreSQL - ORDER BY
- Python PostgreSQL - 更新表
- Python PostgreSQL - 删除数据
- Python PostgreSQL - 删除表
- Python PostgreSQL - LIMIT
- Python PostgreSQL - JOIN
- Python PostgreSQL - 游标对象
- Python SQLite
- Python SQLite - 简介
- Python SQLite - 建立连接
- Python SQLite - 创建表
- Python SQLite - 插入数据
- Python SQLite - 查询数据
- Python SQLite - WHERE 子句
- Python SQLite - ORDER BY
- Python SQLite - 更新表
- Python SQLite - 删除数据
- Python SQLite - 删除表
- Python SQLite - LIMIT
- Python SQLite - JOIN
- Python SQLite - 游标对象
- Python MongoDB
- Python MongoDB - 简介
- Python MongoDB - 创建数据库
- Python MongoDB - 创建集合
- Python MongoDB - 插入文档
- Python MongoDB - 查找
- Python MongoDB - 查询
- Python MongoDB - 排序
- Python MongoDB - 删除文档
- Python MongoDB - 删除集合
- Python MongoDB - 更新
- Python MongoDB - LIMIT
- Python 数据访问资源
- Python 数据访问 - 快速指南
- Python 数据访问 - 有用资源
- Python 数据访问 - 讨论
Python MongoDB - 排序
检索集合内容时,可以使用sort()方法对集合进行排序,并按升序或降序排列。
此方法可以传入字段和排序顺序(1 或 -1)。其中,1 表示升序,-1 表示降序。
语法
以下是sort()方法的语法。
>db.COLLECTION_NAME.find().sort({KEY:1})
示例
假设我们已经创建了一个集合,并向其中插入了 5 个文档,如下所示:
> use testDB switched to db testDB > db.createCollection("myColl") { "ok" : 1 } > data = [ ... {"_id": "1001", "name": "Ram", "age": "26", "city": "Hyderabad"}, ... {"_id": "1002", "name": "Rahim", "age": 27, "city": "Bangalore"}, ... {"_id": "1003", "name": "Robert", "age": 28, "city": "Mumbai"}, ... {"_id": "1004", "name": "Romeo", "age": 25, "city": "Pune"}, ... {"_id": "1005", "name": "Sarmista", "age": 23, "city": "Delhi"}, ... {"_id": "1006", "name": "Rasajna", "age": 26, "city": "Chennai"} ] > db.sample.insert(data) BulkWriteResult({ "writeErrors" : [ ], "writeConcernErrors" : [ ], "nInserted" : 6, "nUpserted" : 0, "nMatched" : 0, "nModified" : 0, "nRemoved" : 0, "upserted" : [ ] })
以下代码行检索集合中的所有文档,这些文档根据年龄按升序排序。
> db.sample.find().sort({age:1}) { "_id" : "1005", "name" : "Sarmista", "age" : 23, "city" : "Delhi" } { "_id" : "1004", "name" : "Romeo", "age" : 25, "city" : "Pune" } { "_id" : "1006", "name" : "Rasajna", "age" : 26, "city" : "Chennai" } { "_id" : "1002", "name" : "Rahim", "age" : 27, "city" : "Bangalore" } { "_id" : "1003", "name" : "Robert", "age" : 28, "city" : "Mumbai" } { "_id" : "1001", "name" : "Ram", "age" : "26", "city" : "Hyderabad" }
使用 Python 对文档进行排序
为了按升序或降序对查询结果进行排序,pymongo 提供了sort()方法。在此方法中,传递一个数值,表示结果中需要的文档数量。
默认情况下,此方法根据指定的字段按升序对文档进行排序。如果需要按降序排序,请将 -1 与字段名称一起传递:
coll.find().sort("age",-1)
示例
以下示例检索集合中的所有文档,并根据年龄值按升序排列:
from pymongo import MongoClient #Creating a pymongo client client = MongoClient('localhost', 27017) #Getting the database instance db = client['b_mydb'] #Creating a collection coll = db['myColl'] #Inserting document into a collection data = [ {"_id": "1001", "name": "Ram", "age": "26", "city": "Hyderabad"}, {"_id": "1002", "name": "Rahim", "age": "27", "city": "Bangalore"}, {"_id": "1003", "name": "Robert", "age": "28", "city": "Mumbai"}, {"_id": "1004", "name": "Romeo", "age": 25, "city": "Pune"}, {"_id": "1005", "name": "Sarmista", "age": 23, "city": "Delhi"}, {"_id": "1006", "name": "Rasajna", "age": 26, "city": "Chennai"} ] res = coll.insert_many(data) print("Data inserted ......") #Retrieving first 3 documents using the find() and limit() methods print("List of documents (sorted in ascending order based on age): ") for doc1 in coll.find().sort("age"): print(doc1)
输出
Data inserted ...... List of documents (sorted in ascending order based on age): {'_id': '1005', 'name': 'Sarmista', 'age': 23, 'city': 'Delhi'} {'_id': '1004', 'name': 'Romeo', 'age': 25, 'city': 'Pune'} {'_id': '1006', 'name': 'Rasajna', 'age': 26, 'city': 'Chennai'} {'_id': '1001', 'name': 'Ram', 'age': '26', 'city': 'Hyderabad'} {'_id': '1002', 'name': 'Rahim', 'age': '27', 'city': 'Bangalore'} {'_id': '1003', 'name': 'Robert', 'age': '28', 'city': 'Mumbai'}
广告