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

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

Name Description 功能
$eq Matches values that are equal to a specified value. 匹配值等于指定值。
$gt Matches values that are greater than a specified value. 匹配值大于指定值。
$gte Matches values that are greater than or equal to a specified value. 匹配值大于等于指定值。
$in Matches any of the values specified in an array. 匹配数组中任意一个值。
$lt Matches values that are less than a specified value. 匹配值小于指定值。
$lte Matches values that are less than or equal to a specified value. 匹配值小于等于指定值。
$ne Matches all values that are not equal to a specified value. 匹配值不等于指定值。
$nin Matches none of the values specified in an array. 匹配数组中任意一个值都不匹配。

代码实例

ts 复制代码
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();
  }
})();
相关推荐
十八旬19 小时前
苍穹外卖项目实战(day7-1)-缓存菜品和缓存套餐功能-记录实战教程、问题的解决方法以及完整代码
java·数据库·spring boot·redis·缓存·spring cache
zheshiyangyang19 小时前
TypeScript学习【一】
javascript·学习·typescript
要一起看日出20 小时前
MVCC-多版本并发控制
数据库·mysql·mvcc
Hx__20 小时前
MySQL InnoDB 的 MVCC 机制
数据库·mysql
速易达网络20 小时前
ASP.NET MVC 连接 MySQL 数据库查询示例
数据库·asp.net·mvc
玉衡子21 小时前
MySQL基础架构全面解析
数据库·后端
梦中的天之酒壶21 小时前
Redis Stack扩展功能
数据库·redis·bootstrap
GreatSQL21 小时前
GreatSQL分页查询优化案例实战
数据库
Leo.yuan21 小时前
不同数据仓库模型有什么不同?企业如何选择适合的数据仓库模型?
大数据·数据库·数据仓库·信息可视化·spark
麦兜*1 天前
MongoDB 6.0 新特性解读:时间序列集合与加密查询
数据库·spring boot·mongodb·spring·spring cloud·系统架构