添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

UncategorizedMongoDbException: Query failed with error code 2 and error message 'unknown top level operator: $gte'

Ask Question

Why I am facing this exception?

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)

Without this code works fine:

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());

Fails also for

//...
.where(field).gte(from).lt(to)//...

But similar code works fine for other cases.

I have tried your code, it is working for return mongo.find(new Query(Criteria.where(field).gte(from).lt(to)), getModelClass());

but not working for

return mongo.find(new Query(Criteria.where(field).gte(from).and(field).lt(to)), getModelClass());

The error I am getting is 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"}}'.

Hence the conclusion is remove .and(field) from the query.

FYI : from = yesterday and to = now for me.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.