所以首先这是我想要做的:
我得到了一个带有一些自定义输入的数据表。用户可以选择技能(php、mysql等)和标准文本输入。用户可以选择多个技能。When multiple skills are selected it's supposed to be kind of an AND AND query. 所以这两种技能都必须可用。
数据表中的标准文本输入将包含诸如客户姓名、他们的电子邮件或工作职能等信息。
例如,我检查了 PHP 和 Javascript,在我的文本搜索中我使用了 Google。这意味着我正在寻找在标签中同时包含 PHP 和 Javascript 的文档(或它们的任何别名,例如:Javascript => js),并且在列出的任何其他字段(客户、函数、发件人姓名、发件人电子邮件或主题)。
但它也应该能够搜索仅匹配技能或仅用于文本输入的文档。
我一直在尝试使用所有查询选项构建数组,并根据函数的输入合并这些数组,但到目前为止没有任何效果。
匹配过滤器必须是对象中的表达式
下面是我在 Mongo 中获得的文档示例
{
"_id": {
"$oid": "superfancyID"
},
"parsed": {
"tags": [
"sql",
"php"
],
"customer": "CustomerName",
"function": "functionName",
"mail_properties": {
"senderName": "SenderName",
"senderEmail": "example@example.com",
"subject": "FW: Test email",
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是我尝试构建查询的一些代码
public function handler(int $skip, int $limit, array $filters, string $filterString = '') {
# $filters are skills
# $filterString is a string of everything else you put in the search field of the datatable
/** @var \MongoDB\Collection $collection */
$collection = $this->emailRepository->getCollection();
/** @var array $regex */
$regex = [];
if(!empty($filterString))
{
$strings = explode(' ', $filterString);
foreach($strings as $item)
{
$regex[] = new Regex($item, 'i');
}
}
#This function basically does the same as above.
#The only change is that this also goes over any aliases for skills. (Javascript => js)
/** @var array $aliasRegex */
$aliasRegex = $this->createAliasRegex($filters);
$skillsFilters = [];
#This is where I attempt to create a part of the query for just the skills.
if(!empty($aliasRegex)) {
$skillsFilters = [
'$and' => [
'$or' => [
['parsed.tags' => [
'$in' => $aliasRegex
]]
]
]
];
}
$stringFilters = [];
#This is where I try and build the rest of the query.
if(!empty($regex)) {
$stringFilters = ['$or' =>
['parsed.function' => [
'$in' => $regex
]],
['parsed.mail_properties.subject' => [
'$in' => $regex
]],
['parsed.customer' => [
'$in' => $regex
]]
];
}
$documents = $collection->aggregate([
[
'$match' => [
array_merge($stringFilters, $aliasFilters)
]
]
,[
'$limit' => $limit,
],[
'$skip' => $skip
]
]);
Run Code Online (Sandbox Code Playgroud)
这基本上是我得到的全部,我尝试检查我的阵列并移动一些部分,但没有任何效果对我有用。我一直在找回错误
匹配过滤器必须是对象中的表达式