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();
  }
})();
相关推荐
m0_748554812 小时前
golang如何实现用户订阅偏好管理_golang用户订阅偏好管理实现总结
jvm·数据库·python
紫微AI2 小时前
前端文本测量成了卡死一切创新的最后瓶颈,pretext实现突破了
前端·人工智能·typescript
早日退休!!!3 小时前
《数据结构选型指南》笔记
数据结构·数据库·oracle
xcLeigh3 小时前
KES数据库性能优化实战
数据库·sql·性能优化·sql优化·数据性能
阿正呀3 小时前
Redis怎样实现本地缓存的高效失效通知
jvm·数据库·python
yoyo_zzm3 小时前
Laravel9.x新特性全解析
数据库·mysql·nginx
2501_901200533 小时前
mysql如何设置InnoDB引擎参数_优化innodb_buffer_pool
jvm·数据库·python
若梦plus4 小时前
TypeScript进阶
前端·javascript·typescript·ecmascript
m0_495496414 小时前
mysql处理复杂SQL性能_InnoDB优化器与MyISAM差异
jvm·数据库·python
forEverPlume5 小时前
PHP怎么使用Eloquent Attribute Composition属性组合_Laravel通过组合构建复杂属性【方法】
jvm·数据库·python