简体   繁体   English

如何进行多过滤搜索?

[英]How Do I Make a Multi-filter Search?

I'm building an ecommerce site with Vanilla JS, Node and MongoDB.我正在用 Vanilla JS、Node 和 MongoDB 构建一个电子商务网站。 My search bar currently can only return searches based upon the name of the product as follows:我的搜索栏目前只能根据产品名称返回搜索,如下所示:

productRouter.get('/', expressAsyncHandler(async (req, res) => {
    const searchKeyword = req.query.searchKeyword
    ? {
        name: {
            $regex: req.query.searchKeyword,
            $options: 'i'
            }
        }
    : {};
    const products = await Product.find({...searchKeyword});
    res.send(products);
    })
);

I've tried adding category and brand inside of the ?我尝试在 ? 中添加类别和品牌? {} like so to no avail: {} 这样无济于事:

? {
        name: {
            $regex: req.query.searchKeyword,
            $options: 'i'
            }, 
        category: {
            $regex: req.query.searchKeyword,
            $options: 'i'
            }, 
        brand: {
            $regex: req.query.searchKeyword,
            $options: 'i'
            }
  }

Thanks!谢谢!

You could use the search text feature.您可以使用搜索文本功能。

It would be something like this:它会是这样的:

// Creating compound index on db
db.product.createIndex({ name: 'text', category: 'text', brand: 'text' })

// Executing query
db.product.find({ $text: { $search: "something" } })

If you're using Mongoose, here is demonstrating how to create an index.如果您使用的是 Mongoose,这里将演示如何创建索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何创建一个多过滤器 function 来过滤掉多个属性? - How to create a multi-filter function to filter out multiple attributes? 如何使用 Object 中的多个条件对 Javascript 中的数组进行多重过滤? - How to Multi-Filter an Array in Javascript with multiple Conditions from Object? 是否有可能在 WebGlPointsLayer 上实现多过滤 - Is there any possibilities to realize multi-filter on WebGlPointsLayer 如何使数据表示例“多过滤器”适应多表支持 - How to adapt datatables example “multi-filter” for multi-table support 如何创建woocommerce / ecommerce产品过滤器之类的多过滤器库 - How to create multi-filter gallery like woocommerce/ecommerce product filter 如何使用过滤器制作搜索栏? - How do I make a search bar with a filter? 如何在JavaScript中使用搜索过滤器和复选框过滤器进行高级过滤器逻辑? - how do I make an advance filter logic using search filter and checkbox filter in javascript? 尝试在AngularJS中创建多重过滤器以按月和年ID过滤JSON数据 - Trying to create multi-filter in AngularJS to filter JSON data by month and year id's 包含字符串的data-attr的多过滤器表行 - Multi-filter table-rows by data-attr containing string 如何进行下拉多选? - How do I make a dropdown multi select?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM