目录

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();
  }
})();
本文是转载文章,点击查看原文
如有侵权,请联系 xyy@jishuzhan.net 删除
相关推荐
我是个假程序员32 分钟前
sql server数据库可疑修复
数据库
记得早睡~2 小时前
leetcode51-N皇后
javascript·算法·leetcode·typescript
极限实验室2 小时前
如何使用 Nginx 代理 Easysearch 服务
数据库·nginx
whn19772 小时前
selectdb修改表副本
数据库
TDengine (老段)3 小时前
TDengine 中的视图
数据库·物联网·oracle·时序数据库·tdengine·iotdb
Kyrie_Li4 小时前
Redis-Sentinel(哨兵模式)
数据库·redis·sentinel
计算机毕设定制辅导-无忧学长4 小时前
TDengine 数据写入优化:协议选择与批量操作(一)
网络·数据库·tdengine
Mr.洛 白4 小时前
OpenEuler/CentOS一键部署OpenGauss数据库教程(脚本+视频)
数据库·opengauss·gaussdb·国产数据库安装·安装脚本
炬火初现5 小时前
redis-cpp-cpp如何使用lua脚本
数据库·redis·lua
hxung5 小时前
Redis 数据类型详解
数据库·redis·缓存