创建一个包含 TextCriteria 和另一个字段的存储库查询
Creating a Repository Query that include TextCriteria AND one other field
我的数据库文档如下所示
@Document(collection = "campaign")
class Campaign {
@Id
var id: String? = null
@Indexed
@TextIndexed(weight = 1f)
var name: String? = null
var mediaLink: String? = null
var imageLink: String? = null
@TextIndexed(weight = 2f)
var text: String? = null
@TextIndexed(weight = 2f)
var target: String? = null
var ownerId: String? = null // Who can modify this except "MANAGER"
var useScope: String? = null // Who can Use this Campaign [me, dl, all] i.e. Visibility
@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
var startOn: Date? = null // @ this date the campaign becomes available and usable
@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
var expireOn: Date? = null // @ this date the campaign becomes expired and not usable
var timestamp: Date? = null
....
存储库
@Repository
interface CampaignRepository : MongoRepository<Campaign, String> {
// page through results for full text query
fun findAllBy(textCriteria: TextCriteria?, pageable: Pageable): Page<Campaign?>?
这实际上对用 @TextIndexed
注释的字段进行了很好的完整搜索
问题是当我添加另一个过滤字段时,就像我想过滤 ownerId
整个查询要么不编译,要么在存储库函数中将 TextCriteria
更改为 String
。
业务逻辑要求我只显示所有者的数据
我试过了
// Expected parameter types: String , String not String, TextCriteria
fun findAllByOwnerId(ownerId: String?, textCriteria: TextCriteria?, pageable: Pageable): Page<Campaign?>?
// Property name must not be null or empty: 'findAllBy<EMPTY_PROPERTY>AndOwnerId'
un findAllByAndOwnerId(ownerId: String, textCriteria: TextCriteria?, pageable: Pageable): Page<Campaign?>?
// No property findAllOwnerId found for type Campaign! during Compiletime
fun findAllOwnerId(ownerId: String, textCriteria: TextCriteria?, pageable: Pageable): Page<Campaign?>?
我放弃!请协助
在Java的情况下,可以在仓库中使用@Query
注解,在where子句中传递需要的字段,如spring-data的官方文档-mongodb
6.3.2。 MongoJSON基于查询方式和字段限制:https://docs.spring.io/spring-data/data-document/docs/current/reference/html/
By adding the annotation org.springframework.data.mongodb.repository.Query repository finder methods you can specify a Mongo JSON query string to use instead of having the query derived from the method name.
对于 and
子句中带有额外参数和参数的文本搜索,包括可分页参数
@Query("{$and:[ {'ownerId': ?0} , {'$text' : { '$search' : ?1}} ]}")
Page<Campaign> searchByByOwnerIdAndText(String ownerId, String keywords,
Pageable page);
上述查询的调试日志为:
2019-12-29 14:03:42.053 DEBUG 10998 --- [ main] o.s.d.m.r.query.StringBasedMongoQuery : Created query Document{{$and=[Document{{ownerId=12}}, Document{{$text=Document{{$search=Krishna}}}}]}} for Document{{}} fields.
以上代码的详细内容,您可以访问Github页面:https://github.com/krishnaiitd/learningJava/blob/master/spring-boot-sample-data-mongodb/src/main/java/sample/data/mongo/repository/CampaignRepository.java#L11
希望这对您的 Kotlin 代码也有帮助。
我的数据库文档如下所示
@Document(collection = "campaign")
class Campaign {
@Id
var id: String? = null
@Indexed
@TextIndexed(weight = 1f)
var name: String? = null
var mediaLink: String? = null
var imageLink: String? = null
@TextIndexed(weight = 2f)
var text: String? = null
@TextIndexed(weight = 2f)
var target: String? = null
var ownerId: String? = null // Who can modify this except "MANAGER"
var useScope: String? = null // Who can Use this Campaign [me, dl, all] i.e. Visibility
@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
var startOn: Date? = null // @ this date the campaign becomes available and usable
@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss.SSSXXX")
var expireOn: Date? = null // @ this date the campaign becomes expired and not usable
var timestamp: Date? = null
....
存储库
@Repository
interface CampaignRepository : MongoRepository<Campaign, String> {
// page through results for full text query
fun findAllBy(textCriteria: TextCriteria?, pageable: Pageable): Page<Campaign?>?
这实际上对用 @TextIndexed
注释的字段进行了很好的完整搜索
问题是当我添加另一个过滤字段时,就像我想过滤 ownerId
整个查询要么不编译,要么在存储库函数中将 TextCriteria
更改为 String
。
业务逻辑要求我只显示所有者的数据
我试过了
// Expected parameter types: String , String not String, TextCriteria
fun findAllByOwnerId(ownerId: String?, textCriteria: TextCriteria?, pageable: Pageable): Page<Campaign?>?
// Property name must not be null or empty: 'findAllBy<EMPTY_PROPERTY>AndOwnerId'
un findAllByAndOwnerId(ownerId: String, textCriteria: TextCriteria?, pageable: Pageable): Page<Campaign?>?
// No property findAllOwnerId found for type Campaign! during Compiletime
fun findAllOwnerId(ownerId: String, textCriteria: TextCriteria?, pageable: Pageable): Page<Campaign?>?
我放弃!请协助
在Java的情况下,可以在仓库中使用@Query
注解,在where子句中传递需要的字段,如spring-data的官方文档-mongodb
6.3.2。 MongoJSON基于查询方式和字段限制:https://docs.spring.io/spring-data/data-document/docs/current/reference/html/
By adding the annotation org.springframework.data.mongodb.repository.Query repository finder methods you can specify a Mongo JSON query string to use instead of having the query derived from the method name.
对于 and
子句中带有额外参数和参数的文本搜索,包括可分页参数
@Query("{$and:[ {'ownerId': ?0} , {'$text' : { '$search' : ?1}} ]}")
Page<Campaign> searchByByOwnerIdAndText(String ownerId, String keywords,
Pageable page);
上述查询的调试日志为:
2019-12-29 14:03:42.053 DEBUG 10998 --- [ main] o.s.d.m.r.query.StringBasedMongoQuery : Created query Document{{$and=[Document{{ownerId=12}}, Document{{$text=Document{{$search=Krishna}}}}]}} for Document{{}} fields.
以上代码的详细内容,您可以访问Github页面:https://github.com/krishnaiitd/learningJava/blob/master/spring-boot-sample-data-mongodb/src/main/java/sample/data/mongo/repository/CampaignRepository.java#L11
希望这对您的 Kotlin 代码也有帮助。