- 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}]
广告
