春季孟戈查询集合属性与下划线字符
问题描述:
我正在构建一个查询来检索使用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”的集合,这很重要,因为我的数据库中存在一个名称为“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
项是一个数组与集合格式如下:
{
"instanceId" : "654ba2d16579e",
"app_id" : "583471adb0",
"unique_app_id" : "554577506",
"value" : 169
}
值得一提的是,相同的查询(不带下划线代替)在正常工作mongo IDE(Robomongo在这种情况下)。
我正在使用spring-boot-starter-data-mongodb 1.4.1.RELEASE。
我现在真的没有想法。
有什么建议吗?
答
每本Spring Data Commons文档的第3.4.3节:
当我们把强调作为保留字符,我们stongly奉劝 遵循标准的Java命名约定(即不使用下划线在 属性名称,但骆驼代替)。
我不相信你可以在使用Spring的元素名称中间使用下划线字符。手动引用以被引用的集合命名。使用文档类型(以单数形式收集名称),然后使用_id (<document>_id)
。这是您可以在中间使用下划线的唯一情况。
更新:对于您所看到的确切行为以及Spring的bug tracker,下面是现有的pull request。所以
> db.app.findOne({ "entries" : { "$elemMatch" : { "app_id" : "1"}}})
{
"_id" : ObjectId("58a5bc6afa8dd4ae3097d5f7"),
"name" : "Keith",
"entries" : [
{
"instanceId" : "654ba2d16579e",
"app_id" : "1"
}
]
}
,也许当它解析一个条件时,发现多个_
令牌春节API不分裂,但确实:
从蒙戈外壳,我可以成功执行以下查询在解析一个时分割遍历。
[Spring-Data-Jpa Repository - 实体列名称上的下划线]的可能重复(http:// stackoverflow。com/questions/23456197/spring-data-jpa-repository-underscore-on-entity-column-name) – Keith
你可以在你的问题中添加一个'entries'文件的例子吗? – Keith
已被复制到原始问题@Keith。 – guanabara