当前位置: 首页 > news >正文

MongoDB比较查询操作符中英对照表及实例详解

mongodb比较查询操作符中英表格一览表

NameDescription功能
$eqMatches values that are equal to a specified value.匹配值等于指定值。
$gtMatches values that are greater than a specified value.匹配值大于指定值。
$gteMatches values that are greater than or equal to a specified value.匹配值大于等于指定值。
$inMatches any of the values specified in an array.匹配数组中任意一个值。
$ltMatches values that are less than a specified value.匹配值小于指定值。
$lteMatches values that are less than or equal to a specified value.匹配值小于等于指定值。
$neMatches all values that are not equal to a specified value.匹配值不等于指定值。
$ninMatches none of the values specified in an array.匹配数组中任意一个值都不匹配。

代码实例

const mongoose = require('mongoose');// 假设我们在运行代码的电脑已经安装MongoDB,现在链接MongoDB 数据库
//`mongodb://localhost:27017/testdb`这里是数据库地址,testdb 是数据库名称
mongoose.connect('mongodb://localhost:27017/testdb', { useNewUrlParser: true, useUnifiedTopology: true });// 定义 Product 模型
const productSchema = new mongoose.Schema({name: String,price: Number,category: String
});const Product = mongoose.model('Product', productSchema);// 示例数据插入
async function insertSampleData() {await Product.insertMany([{ name: 'Laptop', price: 999, category: 'Electronics' },{ name: 'Smartphone', price: 499, category: 'Electronics' },{ name: 'Coffee Maker', price: 89, category: 'Home Appliances' },{ name: 'Desk Chair', price: 150, category: 'Furniture' }]);
}// 查询示例
async function queryExamples() {// $eq - 匹配价格等于 999 的产品const eqProducts = await Product.find({ price: { $eq: 999 } });console.log('$eq:', eqProducts);// $gt - 匹配价格大于 100 的产品const gtProducts = await Product.find({ price: { $gt: 100 } });console.log('$gt:', gtProducts);// $gte - 匹配价格大于等于 150 的产品const gteProducts = await Product.find({ price: { $gte: 150 } });console.log('$gte:', gteProducts);// $in - 匹配类别在 ['Electronics', 'Furniture'] 中的产品const inProducts = await Product.find({ category: { $in: ['Electronics', 'Furniture'] } });console.log('$in:', inProducts);// $lt - 匹配价格小于 300 的产品const ltProducts = await Product.find({ price: { $lt: 300 } });console.log('$lt:', ltProducts);// $lte - 匹配价格小于等于 150 的产品const lteProducts = await Product.find({ price: { $lte: 150 } });console.log('$lte:', lteProducts);// $ne - 匹配价格不等于 89 的产品const neProducts = await Product.find({ price: { $ne: 89 } });console.log('$ne:', neProducts);// $nin - 匹配类别不在 ['Electronics', 'Home Appliances'] 中的产品const ninProducts = await Product.find({ category: { $nin: ['Electronics', 'Home Appliances'] } });console.log('$nin:', ninProducts);
}// 运行示例
(async () => {try {await insertSampleData();await queryExamples();mongoose.connection.close();} catch (error) {console.error(error);mongoose.connection.close();}
})();
http://www.lryc.cn/news/489800.html

相关文章:

  • 掌上单片机实验室 – RT-Thread + ROS2 初探(25)
  • ‌Kotlin中的?.和!!主要区别
  • iframe嵌入踩坑记录
  • 面试小札:Java的类加载过程和类加载机制。
  • Spring 上下文对象
  • Wireshark抓取HTTPS流量技巧
  • 测试人员--如何区分前端BUG和后端BUG
  • 【Vue】指令扩充(指令修饰符、样式绑定)
  • Ubuntu20.04 Rk3588 交叉编译ffmpeg7.0
  • HTML常用表格与标签
  • 网络安全与加密
  • MySQL数据库-索引的介绍和使用
  • 【图像去噪】论文精读:Pre-Trained Image Processing Transformer(IPT)
  • Java SE 与 Java EE:基础与进阶的探索之旅
  • ssm旅游推荐系统的设计与开发
  • 【人工智能】用Python和NLP工具构建文本摘要模型:使用NLTK和spaCy进行自然语言处理
  • 51c大模型~合集76
  • 资源控制器--laravel进阶篇
  • 对象:是什么,使用,遍历对象,内置对象
  • 设计模式:4、命令模式(双重委托)
  • DataWorks快速入门
  • EasyExcel并行导出多个excel文件并压缩下载
  • 圣诞节秘诀
  • 亚信安全发布《2024年第三季度网络安全威胁报告》
  • Long noncoding RNAs and humandisease
  • 嵌入式AI之rknn yolov5初探
  • 《Vue零基础入门教程》第三课:起步案例
  • 深入浅出C#编程语言
  • 游戏盾 :在线游戏的终极防护屏障
  • 工作中的问题记录笔记