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();
  }
})();
相关推荐
NineData8 分钟前
NineData V5.0 产品发布会:让 AI 成为数据管理的驱动力,4月16日!
数据库·人工智能·ai编程
高梦轩1 小时前
PG数据库
数据库·oracle
云草桑1 小时前
DBA mssql 解决排序规则冲突 QA prod 和开发配置都是一样的服务器排序规则 为啥开发环境的的存储过程需要 加这个COLLATE Chinese_PRC_CI_AS
数据库·dba·mssql
卤炖阑尾炎1 小时前
MySQL 故障排查与生产环境优化实战指南
数据库·mysql
小陈工1 小时前
2026年4月2日技术资讯洞察:数据库融合革命、端侧AI突破与脑机接口产业化
开发语言·前端·数据库·人工智能·python·安全
solihawk2 小时前
分区大表统计信息不准确引发的性能问题
数据库
百结2142 小时前
postgresql日常运用
数据库·postgresql·oracle
前进的李工3 小时前
MySQL大小写规则与存储引擎详解
开发语言·数据库·sql·mysql·存储引擎
CoovallyAIHub3 小时前
Sensors 2026 | 从无人机拍摄到跑道缺陷地图,机场巡检全流程自动化——Zadar机场全跑道验证
数据库·架构·github
炸炸鱼.4 小时前
PostgreSQL 日常维护速查手册
数据库·oracle