为什么我要面对这个例外?
javax.servlet.ServletException: org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.data.mongodb.UncategorizedMongoDbException: Query failed with error code 2 and error message 'unknown top level operator: $gte' on server localhost:27017; nested exception is com.mongodb.MongoQueryException: Query failed with error code 2 and error message 'unknown top level operator: $gte' on server localhost:27017
at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:146)
at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:132)
at org.eclipse.jetty.server.Server.handle(Server.java:564)
at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:317)
at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:251)
at org.eclipse.jetty.io.AbstractConnection$ReadCallback.succeeded(AbstractConnection.java:279)
at org.eclipse.jetty.io.FillInterest.fillable(FillInterest.java:110)
at org.eclipse.jetty.io.ChannelEndPoint$2.run(ChannelEndPoint.java:124)
at org.eclipse.jetty.util.thread.Invocable.invokePreferred(Invocable.java:128)
at org.eclipse.jetty.util.thread.Invocable$InvocableExecutor.invoke(Invocable.java:222)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.doProduce(EatWhatYouKill.java:294)
at org.eclipse.jetty.util.thread.strategy.EatWhatYouKill.produce(EatWhatYouKill.java:126)
at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:673)
at org.eclipse.jetty.util.thread.QueuedThreadPool$2.run(QueuedThreadPool.java:591)
at java.lang.Thread.run(Thread.java:748)
如果没有此代码可以正常工作:
public List<Model> getByPeriod(String field, Date from, Date to) {
return mongo.find(new Query(Criteria.where(field).gte(from)
.and(field).lt(to)), getModelClass());
}
也失败了
//...
.where(field).gte(from).lt(to)//...
但类似的代码适用于其他情况。
答案 0 :(得分:0)
我已尝试过您的代码,它适用于return mongo.find(new Query(Criteria.where(field).gte(from).lt(to)), getModelClass());
但不适合
return mongo.find(new Query(Criteria.where(field).gte(from).and(field).lt(to)), getModelClass());
我得到的错误是Due to limitations of the com.mongodb.BasicDBObject, you can't add a second 'field' expression specified as 'field : { "$lt" : { "$date" : "2017-11-03T12:36:54.216Z"}}'. Criteria already contains 'field : { "$gte" : { "$date" : "2017-11-02T12:36:54.216Z"}}'.
因此,结论是从查询中删除.and(field)
。
仅供参考:从=昨天到现在为我。