
- 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 連線查詢 (Join)
- 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 資料限制 (Limit)
- Node.js - MongoDB 連線查詢 (Join)
- Node.js 模組
- Node.js - 模組
- Node.js - 內建模組
- Node.js - 實用程式模組
- Node.js - Web 模組
- Node.js 有用資源
- Node.js - 快速指南
- Node.js - 有用資源
- Node.js - 討論
Node.js - MongoDB 查詢
在 Node.js 的 mongodb 驅動程式模組中定義的 find() 和 findOne() 方法返回滿足查詢引數的指定集合中的所有文件或第一個文件。您可以使用邏輯運算子構建查詢物件中的過濾器,如下所示:
MongoDB 運算子
MongoDB 不使用傳統的邏輯運算子符號。相反,它有自己的一套運算子,如下所示:
序號 | MongoDB 運算子和描述 |
---|---|
1 | $eq 等於 (==) |
2 | $gt 大於 (>) |
3 | $gte 大於或等於 (>=) |
4 | $in 如果等於陣列中的任何值 |
5 | $lt 小於 (<) |
6 | $lte 小於或等於 (<=) |
7 | $ne 不等於 (!=) |
8 | $nin 如果不等於陣列中的任何值 |
這些運算子用於 find() 方法應用過濾器。以下語句返回價格 > 10000 的產品
示例
const {MongoClient} = require('mongodb'); async function main(){ const uri = "mongodb://:27017/"; const client = new MongoClient(uri); try { // Connect to the MongoDB cluster await client.connect(); // Make the appropriate DB calls // Create a single new listing await fetchdocs(client, "mydb", "products"); } finally { // Close the connection to the MongoDB cluster await client.close(); } } main().catch(console.error); async function fetchdocs(client, dbname, colname){ const result = await client.db(dbname).collection(colname).find({"price":{$gt:10000}}).toArray(); console.log(JSON.stringify(result)); }
輸出
[{"_id":"6580964f20f979d2e9a72ae7","ProductID":1,"Name":"Laptop","price":25000},{"_id":"6580964f20f979d2e9a72ae8","ProductID":2,"Name":"TV","price":40000}]
可以使用 $and 和 $or 運算子進行復合邏輯表示式。使用方法如下:
db.collection.find($and:[{"key1":"value1"}, {"key2":"value2"}])
使用以下命令獲取價格在 1000 到 10000 之間的產品。
async function fetchdocs(client, dbname, colname){ const result = await client.db(dbname).collection(colname).find({$and:[{"price":{$gt:1000}}, {"price":{$lt:10000}}]}).toArray(); console.log(JSON.stringify(result)); }
輸出
[{"_id":"6580964f20f979d2e9a72ae9","ProductID":3,"Name":"Router","price":2000},{"_id":"6580964f20f979d2e9a72aea","ProductID":4,"Name":"Scanner","price":5000},{"_id":"6580964f20f979d2e9a72aeb","ProductID":5,"Name":"Printer","price":9000}]
正則表示式
您還可以透過建立正則表示式來建立過濾器。$regex 變數用作查詢 JSON 表示中的鍵。以下程式碼返回名稱以 P 開頭的所有產品。
示例
async function fetchdocs(client, dbname, colname){ const result = await client.db(dbname).collection(colname).find({Name:{$regex:"^P"}}).toArray(); console.log(JSON.stringify(result)); }
輸出
[{"_id":"6580964f20f979d2e9a72aeb","ProductID":5,"Name":"Printer","price":9000}]
在以下示例中,結果集包含名稱以 Ro 開頭的文件。
async function fetchdocs(client, dbname, colname){ const result = await client.db(dbname).collection(colname).find({Name: /Ro/}).toArray(); console.log(JSON.stringify(result)); }
輸出
[{"_id":"6580964f20f979d2e9a72ae9","ProductID":3,"Name":"Router","price":2000}]
要獲取名稱以 er 結尾的產品,請在末尾使用 $ 符號。
async function fetchdocs(client, dbname, colname){ const result = await client.db(dbname).collection(colname).find({Name: /er$/}).toArray(); console.log(JSON.stringify(result)); }
輸出
[{"_id":"6580964f20f979d2e9a72ae9","ProductID":3,"Name":"Router","price":2000},{"_id":"6580964f20f979d2e9a72aea","ProductID":4,"Name":"Scanner","price":5000},{"_id":"6580964f20f979d2e9a72aeb","ProductID":5,"Name":"Printer","price":9000}]
廣告