
- Node.js 教程
- Node.js - 首頁
- Node.js - 簡介
- Node.js - 環境設定
- Node.js - 第一個應用程式
- Node.js - REPL 終端
- Node.js - 命令列選項
- Node.js - 包管理器 (NPM)
- Node.js - 回撥函式概念
- Node.js - 上傳檔案
- Node.js - 傳送電子郵件
- Node.js - 事件
- Node.js - 事件迴圈
- Node.js - 事件發射器
- Node.js - 偵錯程式
- Node.js - 全域性物件
- Node.js - 控制檯
- Node.js - 程序
- Node.js - 應用程式擴充套件
- Node.js - 打包
- Node.js - Express 框架
- Node.js - RESTFul API
- Node.js - 緩衝區
- Node.js - 流
- Node.js - 檔案系統
- Node.js MySQL
- Node.js - MySQL 入門
- Node.js - MySQL 建立資料庫
- Node.js - MySQL 建立表
- Node.js - MySQL 插入資料
- Node.js - MySQL 從表中選擇資料
- Node.js - MySQL Where 條件
- Node.js - MySQL Order By 排序
- Node.js - MySQL 刪除資料
- Node.js - MySQL 更新資料
- Node.js - MySQL 聯接
- Node.js MongoDB
- Node.js - MongoDB 入門
- Node.js - MongoDB 建立資料庫
- Node.js - MongoDB 建立集合
- Node.js - MongoDB 插入資料
- Node.js - MongoDB 查詢資料
- Node.js - MongoDB 查詢
- Node.js - MongoDB排序
- Node.js - MongoDB 刪除資料
- Node.js - MongoDB 更新資料
- Node.js - MongoDB 限制結果
- Node.js - MongoDB 聯接
- Node.js 模組
- Node.js - 模組
- Node.js - 內建模組
- Node.js - 實用程式模組
- Node.js - Web 模組
- Node.js 有用資源
- Node.js - 快速指南
- Node.js - 有用資源
- Node.js - 討論
Node.js - MongoDB排序
在 MongoDB 資料庫中的集合上執行 find() 查詢的結果,可以按照文件中某個特定欄位的升序或降序排列。Node.js 的 MongoDB 驅動程式在 Collection 物件中定義了 sort() 方法。
要對 Node.js 中 MongoDB 查詢的結果進行排序,可以使用 sort() 方法。此方法允許您按一個或多個欄位的值以特定方向對返回的文件進行排序。要按欄位的升序(從小到大)排序返回的文件,請使用值 1。要改為按降序(從大到小)排序,請使用 -1。
升序排序的語法如下:
result = col.find(query).sort(field:1);
降序排序的語法如下:
result = col.find(query).sort(field:-1);
升序排序
以下示例顯示了 products 集合中的文件,按價格升序排列。
示例
const {MongoClient} = require('mongodb'); async function main(){ const uri = "mongodb://:27017/"; const client = new MongoClient(uri); try { await client.connect(); await sortdocs(client, "mydb", "products"); } finally { await client.close(); } } main().catch(console.error); async function sortdocs(client, dbname, colname){ var mysort = { price: 1 }; const result = await client.db(dbname).collection(colname).find({}).sort(mysort).toArray(); result.forEach(element => { console.log(element); }); }
輸出
{ _id: new ObjectId('6580964f20f979d2e9a72ae9'), ProductID: 3, Name: 'Router', price: 2000 } { _id: new ObjectId('6580964f20f979d2e9a72aea'), ProductID: 4, Name: 'Scanner', price: 5000 } { _id: new ObjectId('6580964f20f979d2e9a72aeb'), ProductID: 5, Name: 'Printer', price: 9000 } { _id: new ObjectId('65809214693bd4622484dce3'), ProductID: 1, Name: 'Laptop', Price: 25000 } { _id: new ObjectId('6580964f20f979d2e9a72ae8'), ProductID: 2, Name: 'TV', price: 40000 }
降序排序
要從 products 集合中生成按名稱欄位降序排列的文件列表,請將 sortdocs() 函式更改為以下內容:
示例
async function sortdocs(client, dbname, colname){ var mysort = { Name: -1 }; const result = await client.db(dbname).collection(colname).find({}).sort(mysort).toArray(); result.forEach(element => { console.log(element); }); });
輸出
{ _id: new ObjectId('6580964f20f979d2e9a72ae8'), ProductID: 2, Name: 'TV', price: 40000 } { _id: new ObjectId('6580964f20f979d2e9a72aea'), ProductID: 4, Name: 'Scanner', price: 5000 } { _id: new ObjectId('6580964f20f979d2e9a72ae9'), ProductID: 3, Name: 'Router', price: 2000 } { _id: new ObjectId('6580964f20f979d2e9a72aeb'), ProductID: 5, Name: 'Printer', price: 9000 } { _id: new ObjectId('65809214693bd4622484dce3'), ProductID: 1, Name: 'Laptop', Price: 25000 }
廣告