Mongodb Mongoose复杂查询用于搜索

Mongodb Mongoose complex query for search

本文关键字:用于 搜索 查询 复杂 Mongoose Mongodb      更新时间:2023-09-26

我正在尝试实现一个动态搜索栏,它根据用户键入的内容显示建议。例如,您开始键入"j",然后会看到诸如"Java,JavaScript,JQuery"之类的选项。。。然后键入"ja",只看到"Java,JavaScript"。

我的模式如下:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var QuestionSchema = new Schema({
    text : String,
    answers : Array,
    tech : String,
    tags : [String],
    level : String,
    createdAt : Date
});
// Return the model
module.exports = mongoose.model('Questions', QuestionSchema);

我需要搜索"文本"、"技术"、"标签"answers"级别"中的匹配项。

到了

let query = [
        { 'text': { $regex: new RegExp(keyword, "i") } },
        { 'tech': { $regex: new RegExp(keyword, "i") } },
        { 'tags': { $regex: new RegExp(keyword, "i") } },
        { 'level': { $regex: new RegExp(keyword, "i") } }
    ]
    Question.find({ $or: query }, function(err, results){
...
    });