NoteZ_技术博客 NoteZ_技术博客
🏠 首页
  • 📚 Web技术
  • 📋 Npm笔记
  • 📑 Markdown
  • 📄 Git笔记
  • 📝 Nginx文档
  • 📓 Linux文档
  • 📖 技术文档
  • 📜 其他文档
  • 🧊 NodeJs
  • 🎡 Express
  • 🔥 Rust
  • 🎉 Koa2
  • 🍃 MongoDB
  • 🐬 MySql
  • 🥦 Oracle
  • 🍁 Python
  • 🍄 JavaScript
  • 🌰 CSS
  • 🧄 HTML
  • 🥑 Canvas
  • 🌽 Nuxt
  • 🍆 React
  • 🥜 Vue
  • 🧅 TypeScript
  • 🌶️ AI
  • 📘 分类
  • 📗 标签
  • 📙 归档
⚜️ 在线编辑 (opens new window)
  • 📁 站点收藏
  • 📦 前端组件库
  • 📊 数据可视化
  • 🌈 开源插件
  • 🎗️ 关于我
  • 🔗 友情链接
GitHub (opens new window)

NoteZ_技术博客

前端界的小学生
🏠 首页
  • 📚 Web技术
  • 📋 Npm笔记
  • 📑 Markdown
  • 📄 Git笔记
  • 📝 Nginx文档
  • 📓 Linux文档
  • 📖 技术文档
  • 📜 其他文档
  • 🧊 NodeJs
  • 🎡 Express
  • 🔥 Rust
  • 🎉 Koa2
  • 🍃 MongoDB
  • 🐬 MySql
  • 🥦 Oracle
  • 🍁 Python
  • 🍄 JavaScript
  • 🌰 CSS
  • 🧄 HTML
  • 🥑 Canvas
  • 🌽 Nuxt
  • 🍆 React
  • 🥜 Vue
  • 🧅 TypeScript
  • 🌶️ AI
  • 📘 分类
  • 📗 标签
  • 📙 归档
⚜️ 在线编辑 (opens new window)
  • 📁 站点收藏
  • 📦 前端组件库
  • 📊 数据可视化
  • 🌈 开源插件
  • 🎗️ 关于我
  • 🔗 友情链接
GitHub (opens new window)
  • Express

  • Koa2

  • MongoDB

    • linux 下 mongodb 的安装及配置
    • MongoDB 备份(mongodump)与恢复(mongorestore)
    • MongoDB 插入(insert)数据的正确方法
    • MongoDB 数据库
    • MongoDB 数据库设置账号密码(权限)
    • MongoDB集合数据查询总结
    • Mongoose 动态给 Schema 添加属性
    • Mongoose 数据验证
    • NodeJs 连接操作 MongoDB 数据库
    • Node下使用MongoDB 数据库多条件模糊查询
    • 使用 JS 操作 MongoDB 命令
    • 如何解决Mongodb大数据量分页查询效率问题
    • 给MongoDB数据库bin目录添加工具集
  • MySql

  • NodeJs

  • Oracle

  • Rust

  • Python

  • 后端开发
  • MongoDB
NoteZ
2021-12-07

Node下使用MongoDB 数据库多条件模糊查询

主要用到了query.$or和query.$regex这两个find参数。 其中query.$or用于实现多条件查询,其值是一个数组,相关文档 (opens new window)

示例代码:

let mongoose = require('mongoose');
let Schema = mongoose.Schema;
let SchemaClass = new Schema({
    name: {
        type: String,
        default: ""
    },
    type: {
        type: String,
        default: ""
    }
});

let regExp = '动态值';
let filterArr = [
    {name: {$regex: new RegExp(`${regExp}`, 'gi')}},
    {type: {$regex: 'class'}}
];

let dataBase = mongoose.model('cshisas', SchemaClass);
dataBase.find({ $or: filterArr }, (err, data) => {
    if (err) {
         console.log(err);
    } else {
        console.log(data);
        //  JSON.parse(JSON.stringify(data))
        //  res.json({ data, result: true, code: 200 });
    
    }
});
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#MongoDB
上次更新: 2024/01/30, 00:35:17
NodeJs 连接操作 MongoDB 数据库
使用 JS 操作 MongoDB 命令

← NodeJs 连接操作 MongoDB 数据库 使用 JS 操作 MongoDB 命令→

最近更新
01
Gitea数据备份与还原
03-10
02
Linux 中使用 rsync 同步文件目录教程
03-10
03
Linux 使用 rsync 互相传输同步文件的简单步骤
03-08
更多文章>
Theme by Vdoing | Copyright © 2019-2025 NoteZ,All rights reserved | 冀ICP备2021027292号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式