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();
  }
})();
相关推荐
一 乐1 小时前
民宿|基于java的民宿推荐系统(源码+数据库+文档)
java·前端·数据库·vue.js·论文·源码
BillKu1 小时前
Vue3 + TypeScript + Element Plus 表格行按钮不触发 row-click 事件、不触发勾选行,只执行按钮的 click 事件
vue.js·elementui·typescript
美林数据Tempodata2 小时前
大模型驱动数据分析革新:美林数据智能问数解决方案破局传统 BI 痛点
数据库·人工智能·数据分析·大模型·智能问数
野槐3 小时前
node.js连接mysql写接口(一)
数据库·mysql
Zzzone6833 小时前
PostgreSQL日常维护
数据库·postgresql
chxii3 小时前
1.13使用 Node.js 操作 SQLite
数据库·sqlite·node.js
冰刀画的圈3 小时前
修改Oracle编码
数据库·oracle
这个胖子不太裤3 小时前
Django(自用)
数据库·django·sqlite
麻辣清汤4 小时前
MySQL 索引类型及其必要性与优点
数据库·mysql
霸王蟹5 小时前
前端项目Excel数据导出同时出现中英文表头错乱情况解决方案。
笔记·学习·typescript·excel·vue3·react·vite