【发布时间】:2020-10-12 21:54:54
【问题描述】:
我制作了以下过滤器,希望将所有 3 个 $and 数组的结果组合在一起,但它只匹配其中一个块。
如果满足条件,我如何组合每个 $and 数组返回的结果。希望这很清楚。 $and 数组我不知道该怎么称呼。
const filter = {
$or: [
{
$and: [
{ category: req.query.category },
{ tags: req.query.subCategory },
{contentType: req.query.contentType},
req.query.searchTerm !== ""
? {
name: {
$regex: "(?i)" + req.query.searchTerm + "(?-i)",
$options: "i",
},
}
: {},
],
$and: [
{ category: req.query.category },
{ tags: req.query.subCategory },
{contentType: req.query.contentType},
req.query.searchTerm !== ""
? {
description: {
$regex: "(?i)" + req.query.searchTerm + "(?-i)",
$options: "i",
},
}
: {},
],
$and: [
{ category: req.query.category },
{ tags: req.query.subCategory },
{contentType: req.query.contentType},
req.query.searchTerm !== ""
? {
tags: {
$regex: "(?i)" + req.query.searchTerm + "(?-i)",
$options: "i",
},
}
: {},
],
},
],
};
await Content.paginate(filter, options, (err, result) => {
if (err) {
res.status(500).send(err);
} else {
res.json(result);
}
});
编辑:下面是可以在数据库中找到的两个条目的示例。它应该工作的方式是它应该使用category、subCategory和contentType过滤掉数据库中的条目,这样我现在只有具有相同category、subCategory的条目, 和contentType 中指定的req.query,我将其称为firstFilterResult。从那里,我试图在firstFilterResult 中搜索,看看我是否有与name、tag 或description 匹配的条目。所以基本上catgeory、subCategory 和contentType 只是用来缩小结果范围,以便我可以找到name、tag 和description 的匹配项。我上面的代码并没有完全做到这一点,但这是它背后的想法,我认为我会做类似的事情,但我想我错了。
contents: [
{
tags: [
'food',
'drinks',
'card',
'account'
],
_id: '1d13ff7m6db4d5417cd608f4',
name: 'THE NAME FOR THIS PIECE OF CONTENT',
description: 'In here I will begin to talk about...',
content_id: '5dbcb998ad4144390c244093',
contentType: 'quiz',
date: '2019-06-03T04:00:00.000Z',
category: 'food',
image: 'https://IMAGE.PNG',
__v: 0
},
{
tags: [
'computer',
'laptop'
],
_id: '7d1b940b1c9d44000025db8c',
name: 'THE NAME FOR THIS PIECE OF CONTENT',
description: 'This is another description',
content_id: '5f1b963d1c9d44000055db8d',
contentType: 'tool',
date: '2019-06-03T04:00:00.000Z',
category: 'money',
image: 'https://IMAGE.PNG',
__v: 0
}
]
【问题讨论】:
-
$or: [ {$and: expr}, {$and: expr}, ... ]应该做你想做的事。也许如果您发布 3 或 4 个代表性文档,我们可以看到问题。另外:$andexprs 看起来一样...? -
每个
$and` expression. tags: { $regex: "(?i)" + req.query.searchTerm + "(?-i)", $options: "i", },都不同,这个有'tag',而其他有'description'和'name' -
而无效的证明是,如果我删除 2 个
and表达式,那么我会得到不同的结果。例如,如果我只有一个具有“标签”的查询,那么我会得到该查询的正确结果,但随后我添加了描述,我丢失了那些原始结果,它们被替换为描述中返回的任何内容 -
我编辑了这个例子,希望这更有意义?
-
尝试在构造
filter之后记录它的值,以确保它看起来确实符合您的预期。
标签: reactjs mongodb mongoose mongodb-query mongojs