我正在尝试使用关键字列表过滤数据库,而我现在的操作方式是在所有模型字段中使用$或对每个字段使用$ in进行检查所有关键字正则表达式的。
key_exps是一个正则表达式数组。在我的测试中,它是一个只有一个元素的数组:/ good / i。
//This is my code that I'm trying to fix:
Ingredient.find({$or:[
{name: {$in: key_exps}},
{number: {$in: key_exps}},
{vendor_info: {$in: key_exps}},
{package_size: {$in: key_exps}},
{cost: {$in: key_exps}},
{comment: {$in: key_exps}}]
}, (err, ingredients) => {
res.json({data: ingredients});
});
//This code works but doesn't allow me to search through multiple keywords:
Ingredient.find({$or:[
{name: {$in: key_exps}},
{number: {$in: key_exps}},
{vendor_info: {$in: key_exps}},
{package_size: {$in: key_exps}},
{cost: {$in: key_exps}},
{comment: key_exps.pop()}]
}, (err, ingredients) => {
res.json({data: ingredients});
});
//This also works but doesn't allow me to search all fields:
Ingredient.find(
{comment: {$in: key_exps}}, (err, ingredients) => {
res.json({data: ingredients});
);