我正在尝试在spring数据中创建mongodb可选查询但是会出错。任何人都可以帮我解决这个问题。
请注意,此查询适用于日期范围
以下是查询
@Query(value = "{ $and: [
{$or : [ { $where: '?0 == null' } , { createdDate : {$gt : ?0} }]},
{$or : [ { $where: '?1 == null' } , { createdDate : {$lt : ?1} }]}
] }")
以下是生成的查询
{ "$and" : [
{ "$or" : [ { "$where" : "{ \"$date\" : \"2016-02-28T18:30:00.000Z\"} == null"} ,
{ "createdDate" : { "$gt" : { "$date" : "2016-02-28T18:30:00.000Z"}}}]} ,
{ "$or" : [ { "$where" : "{ \"$date\" : \"2016-03-11T18:30:00.000Z\"} == null"} ,
{ "createdDate" : { "$lt" : { "$date" : "2016-03-11T18:30:00.000Z"}}}]}
]}
低于错误
org.springframework.data.mongodb.UncategorizedMongoDbException: { "serverUsed" : "localhost:27017" , "waitedMS" : 0 , "ok" : 0.0 , "errmsg" : "Failed to call method" , "code" : 1}; nested exception is com.mongodb.CommandFailureException: { "serverUsed" : "localhost:27017" , "waitedMS" : 0 , "ok" : 0.0 , "errmsg" : "Failed to call method" , "code" : 1}
答案 0 :(得分:1)
您的查询不是很清楚,但根据我的理解(基于?0和?1插值和您的评论):
@Query(value = "{ $or: [
{$and : [ { toDate: { $exists: false } } , { startDate: { $exists: true } }, { createdDate : {$gt : ?0} }]},
{$and : [ { toDate: { $exists: true } }, { startDate: { $exists: false } } , { createdDate : {$lt : ?1} }]},
{$and : [ { toDate: { $exists: false } }, { startDate: { $exists: false } }
] }")