Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Mongoose ORM创建搜索查询_Mongoose - Fatal编程技术网

使用Mongoose ORM创建搜索查询

使用Mongoose ORM创建搜索查询,mongoose,Mongoose,我正在尝试使用Mongoose ORM创建搜索查询。搜索引擎可以找到零件号、零件说明或NSN号。但是,我不知道用户要搜索这三个选项中的哪一个。我不知道如何最好地实现这个解决方案,或者它是否有可能与猫鼬 let mongoose = require('mongoose'); let mongooseUniqueValidator = require('mongoose-unique-validator'); // Global variables let Schema = mongoose.Sc

我正在尝试使用Mongoose ORM创建搜索查询。搜索引擎可以找到零件号、零件说明或NSN号。但是,我不知道用户要搜索这三个选项中的哪一个。我不知道如何最好地实现这个解决方案,或者它是否有可能与猫鼬

let mongoose = require('mongoose');
let mongooseUniqueValidator = require('mongoose-unique-validator');

// Global variables
let Schema = mongoose.Schema;

// Define part schema
let schema = new Schema({
    // _id: mongoose.Schema.Types.ObjectId,
    nsn: { type: String, trim: true, unique: true, minlength: 1 },
    partList: { type: String, trim: true },
    description: { type: String, trim: true },
    dataSheet: [{
        mrc: { type: String, trim: true },
        tipText: { type: String, trim: true },
        category: { type: String, trim: true },
        categoryDescription: { type: String, trim: true },
        url: { type: String, trim: true },
    }],
    category: { type: String, trim: true, minlength: 1 },
    subcategory: { type: String, trim: true, minlength: 1 },
    filter: { type: String, trim: true, minlength: 1 },
    url: { type: String, trim: true },
    createdAt: Date,
    updatedAt: Date
})

// plugin middleware
schema.plugin(mongooseUniqueValidator);

// create indexes
schema.index({nsn: 'text', partList: 'text', description: 'text'});

// Export schema
module.exports = mongoose.model('Part', schema); ```




``` let searchString = req.params.search

    Part.find({$text: {$search: searchString}})
        .then(part => {
            if (part.length > 0) {
                res.json({
                    confirmation: 'success',
                    part: part
                });
            } else {
                res.status(204)
                    .json({

                    })
            }
        })
        .catch(err => {
            res.status(500)
                .json({
                    confirmation: 'fail',
                    message: err.message
                });
        }) ```

所有的信息都会存储在一个字段中吗?不,我需要搜索3个不同的字段如果你是说在一个查询中,那么这有点棘手,我需要它成为一个查询。我已经添加了我的模式和当前查询。此查询正在运行,但我希望它只返回精确匹配的结果。