首页 > 解决方案 > 枚举数组文本不使用 $text 搜索

问题描述

我的架构是

const mongoose = require('mongoose');

const ProductSchema = new mongoose.Schema({
    title: {
        type: String,
        required: true,
        trim: true,
        text: true
    },
    price: {
        type: Number,
        required: true,
        trim: true
    },
    description: {
        type: String,
        required: true,
        trim: true,
        text: true
    },
    productImages: [
        {
            type: String,
            required: true,
            trim: true
        }
    ],
    availability: {
        type: Boolean,
        required: true,
        trim: true
    },
    category: {
        type: String,
        required: true,
        enum: ["mobile","computer","televisions","shoes","headphone","bag","camera","cloth","sports","books"],
        text: true
    }
});

mongoose.model('product', ProductSchema);

我的代码是

exports.ProductSearch = (req,res) => {
    const { data } = req.body;
    const query = { $text: { $search: data } };
    
    Product.find(query)
    .then(res=>{
        console.log(`FIND RESPONSE 1 - `, res);
    })
    .catch(err=>{
        console.log(`FIND ERROR - `, err)
    }); 
}

标签: node.jsmongoose

解决方案


推荐阅读