我正在尝试搜索与 json 对象匹配的数据库行。
示例:
var searchParams = {
city: ['New York City', 'Budapest'],
jobs: ['Assistent'] //Can be multiple
}
//If "city" is equal to one of the searchParams.city variables
//AND jobs is equal to one of the searchParams.jobs variables
var searchQuery = {
$and: [
{$or: clean_city},
{$or: clean_cat}
]
}
Jobs.find(searchQuery, function(err, list){
console.log(list);
});
我已经尝试了在谷歌上可以找到的所有选项,但似乎没有任何选项适用于数组。
如果有人能提供帮助,我将非常感激。
问候
最佳答案
你可以找到一个例子here在您的上下文中,这使得假设:
- 使用
searchParams
因为我不知道clean_*
变量中有什么 - 数据库字段为
city
和job
{
$and: [
{ $or: searchParams.city.map(city => ({ city })) },
{ $or: searchParams.jobs.map(job => ({ job })) }
]
}
这是更短的语法(如果数据库字段名称不同,您无法简化它):
{
$and: [
{ $or: searchParams.city.map((city) => { return { city: city }; }) },
{ $or: searchParams.city.map((job) => { return { job: job }; }) }
]
}
关于javascript - Mongoose.Find() 与数组和 OR 运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54921530/