'executedDate' 是 mongodb 中字符串格式的字段,我需要通过使用 @Query(value="") 将字符串格式解析为日期格式来获取 2 个执行日期之间的记录
我已经在控制台中执行了 mongodb 查询,如下所示:
db.testExecution.aggregate( [ { $project: { date: { $dateFromString: { dateString: '$executionDate' } } } }, { $match: { date: { $lt: ISODate("2019-10-16T06: 35:03Z") } } } , { $match: { date: { $gt: ISODate("2019-09-16T06:55:03Z") } } } ] )
输出:
{ "_id" : ObjectId("5d7f8627ea9c671b877d554f"), "date" : ISODate("2019-09-16T06:55:10Z") }
{ "_id" : ObjectId("5d7f8627ea9c671b877d503f"), "date" : ISODate("2019-10-16T06:25:03Z") }
{ "_id" : ObjectId("5d7f8627ea9c671b877d502f"), "date" : ISODate("2019-09-16T10:55:55Z") }
我需要用 mongoRepository 查询格式写这个,
当我这样写查询时,
@Query(value = "{$project: {date: {$dateFromString: {dateString: '$executionDate'}}}}, {$match: {date: {$lt: ?0)}}} , {$match: {date: {$gt: ?1)}}}")
List<TestResultResponseDomain> findByExecutionDate(Date from, Date to);
我收到如下错误。
Caused by: com.mongodb.MongoQueryException: Query failed with error code 2 and error message 'unknown top level operator: $project' on server localhost:27017
at com.mongodb.operation.FindOperation$1.call(FindOperation.java:722) ~[mongodb-driver-core-3.6.3.jar:na]
at com.mongodb.operation.FindOperation$1.call(FindOperation.java:711) ~[mongodb-driver-core-3.6.3.jar:na]
at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:471) ~[mongodb-driver-core-3.6.3.jar:na]
at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:415) ~[mongodb-driver-core-3.6.3.jar:na]
at com.mongodb.operation.FindOperation.execute(FindOperation.java:711) ~[mongodb-driver-core-3.6.3.jar:na]
at com.mongodb.operation.FindOperation.execute(FindOperation.java:83) ~[mongodb-driver-core-3.6.3.jar:na]
at com.mongodb.Mongo$3.execute(Mongo.java:826) ~[mongodb-driver-3.6.3.jar:na]
at com.mongodb.MongoIterableImpl.execute(MongoIterableImpl.java:130) ~[mongodb-driver-3.6.3.jar:na]
at com.mongodb.MongoIterableImpl.iterator(MongoIterableImpl.java:77) ~[mongodb-driver-3.6.3.jar:na]
at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:2440) ~[spring-data-mongodb-2.0.5.RELEASE.jar:2.0.5.RELEASE]