我正在构建一个查询,使用MongoTemplate从mongo集合中检索元素 . 查询条件包含带有下划线的属性,以某种方式替换为“._”,使查询始终返回0个元素 .
Criteria matchingCriteria = Criteria
.where("entries").elemMatch(Criteria.where("app_id").is(appId))
查看日志我可以看到生成的查询如下:
o.s.data.mongodb.core.MongoTemplate: find using query: { "entries" : { "$elemMatch" : { "app._id" : "5834718ab0"}}} fields: null for class: Ranking in collection: ranking
我已经尝试过使用BasicQuery,使用'\'来削减下划线,并使用unicode“app \ u005Fid” . 它们都没有用 . 重要的是要注意我的数据库中存在名为“app”的集合 .
这种行为看起来并不标准 . 当我使用带有下划线的另一个属性时,该值不会被替换:
Criteria matchingCriteria = Criteria .where("entries").elemMatch(Criteria.where("unique_app_id").is(appId))
日志:
o.s.data.mongodb.core.MongoTemplate find using query: { "entries" : { "$elemMatch" : { "unique_app_id" : "1131706359"}}} fields: null for class: class Ranking in collection: ranking
entries是一个包含以下格式的集合的数组:
{
"instanceId" : "654ba2d16579e",
"app_id" : "583471adb0",
"unique_app_id" : "554577506",
"value" : 169
}
值得一提的是,相同的查询(没有下划线替换)在mongo IDE(在本例中为Robomongo)中工作正常 .
我正在使用spring-boot-starter-data-mongodb 1.4.1.RELEASE .
我现在真的没有想法 .
有什么建议吗?
1 回答
根据Spring Data Commons文件的第3.4.3节:
我使用Spring不知道't believe you can use an underscore character in the middle of an element'的名字 . 手动引用以引用的集合命名 . 使用文档类型(单数集合名称),然后使用
_id ( <document>_id )
. 这是唯一可以在中间使用下划线的情况 .Update :这是一个现有的pull request,其确切的行为是're seeing, as well as Spring' s bug tracker .
从Mongo shell中,我可以成功执行以下查询:
因此,当解析标准时,Spring API在找到多个
_
标记时可能不会拆分,但在解析标准时会解析为遍历 .