我有 mongoDB 集合如下
{
"_id": "ba4d5a0e-1759-4e88-bd57-5e7781d46da7",
"name": "Doc 3 updated",
"documentClass": "033a1f67-b382-43f5-b1e4-7ade26f1522c",
"documentClassName": "Sample Document",
"meta": [
{
"index_field_id": "9e744f36-1c63-45e8-8180-3ceef894b724",
"value": "a"
},
{
"index_field_id": "08afba06-9a7e-4df8-a946-a8cccbefda8b",
"value": "asafsf"
}
],
"version": 2,
"archived": false,
"checkedOut": false,
"checkedOutBy": "svpadm",
"created": {
"$date": "2023-12-24T11:12:39.580Z"
},
"modified": {
"$date": "2024-01-09T19:48:29.785Z"
},
"owner": "79866869-398a-4920-975b-032decc12461",
"ownerClear": "svpadm",
"tenant": "ECMDEV01-T",
"updatedBy": "79866869-398a-4920-975b-032decc12461",
"updatedByClear": "svpadm",
"_class": "de.metasonic.cloud.models.database.Document"
}
然后我想通过
phrase
搜索这个文档,然后想检查以下indexFieldsId
中的任何List<String> indexFieldIds
是否存在于meta
对象数组中,所以我找到了一个查询
@Query("{$and: ["
+ " {$or: ["
+ " {'name': {$regex: ?0, $options: 'i'}}, "
+ " {'documentClass': {$regex: ?0, $options: 'i'}}, "
+ " {'documentClassName': {$regex: ?0, $options: 'i'}}, "
+ " {'workbookId': {$regex: ?0, $options: 'i'}}, "
+ " {'owner': {$regex: ?0, $options: 'i'}}, "
+ " {'ownerClear': {$regex: ?0, $options: 'i'}}, "
+ " {'tenant': {$regex: ?0, $options: 'i'}}, "
+ " {'updatedBy': {$regex: ?0, $options: 'i'}}, "
+ " {'updatedByClear': {$regex: ?0, $options: 'i'}}, "
+ " {'linkedDocuments': {$elemMatch: {$regex: ?0, $options: 'i'}}}, "
+ " {'providerState': {$regex: ?0, $options: 'i'}}, "
+ " {'providerMetaData': {$elemMatch: {$regex: ?0, $options: 'i'}}}"
+ " ]},"
+ " {'meta': {$elemMatch: {'index_field_id': {$in: ?1}}}}"
+ "]}"
)
Page<Document> searchByPhraseAndIndexFieldIds(String searchPhrase, List<String> indexFieldIds, Pageable pageable);
但这给了我一个例外
Exception : org.springframework.data.mongodb.UncategorizedMongoDbException: Query failed with error code 2 with name 'BadValue' and error message '$elemMatch needs an Object' on server localhost:27018; nested exception is com.mongodb.MongoQueryException: Query failed with error code 2 with name 'BadValue' and error message '$elemMatch needs an Object' on server localhost:27018
有人可以帮我吗
使用
$elemMatch
时,您似乎只有一个查询条件。 文档状态:
如果您在
表达式中仅指定单个条件,并且不在$elemMatch
内部使用$not
或$ne
运算符,则可以省略$elemMatch
。$elemMatch
我不使用 Spring,这个错误不会发生在 Node 中,但这意味着你可以更改为:
{'linkedDocuments': {$regex: ?0, $options: 'i'}},
和
{'providerMetaData': {$regex: ?0, $options: 'i'}}"
而不是使用
$elemMatch
。