因此,我正在编写搜索查询,但因错误而卡住了。\
在Mongo Compass中查询此查询将返回正确的结果。
{$or:
[
{'title': {$regex : "aard", $options: 'i'} },
{'tags': {$elemMatch: {$regex : "aard", $options: 'i'} } },
{'categories': {$elemMatch: {$regex : "aard", $options: 'i'} } }
]
}
但是在Spring Boot中使用此查询进行查询会引发错误:
查询失败,错误代码2,错误消息'$ elemMatch需要一个对象'
@Query("{$or: [" +
"{'title': {$regex: ?0, $options: 'i'} }, " +
"{'tags': {$elemMatch: {$regex: ?0, $options: 'i'} } }," +
"{'categories': {$elemMatch: {$regex: ?0, $options: 'i'} } }" +
"] }")
List<Product> search(String input);
具有相同结果的较小示例代码:
@Query("{'categories': {$elemMatch: {$regex: ?0, $options: 'i'} } }")
List<Product> findByCategory(String category);
Mongo文档示例:
_id:1
supplier:DBRef(supplier, [object Object], undefined)
title:"Aardappels"
description:"Verse aardappels"
unitSize:"5KG"
categories:["Groente"]
tags:["Aardappels"]
price:14.99
salePrice:9.99
sale:true
amount:0
sold:0
_class: "nl.hva.ewaserver.models.Product"
这很奇怪,它们都具有相同的查询,但是在Spring Boot中,我使用了参数。
这不应该是问题,因为我还测试了硬编码。