Mongotemplate - request object according to the operator greater than (gt) or less (lt)

When I enter this into my console, it works:

db.posts.find({"_id": {$lt:ObjectId("55732dccf58c555b6d3f1c5a")}}).limit(5).sort({"_id":-1})

When I use mongotemplate, it does not work and returns an empty array:

  @RequestMapping(value="/next", method=RequestMethod.GET)
  public List getNextPost(@RequestParam String next) {
      Query query = new Query();
      query.addCriteria(Criteria.where("_id").lt("55732dccf58c555b6d3f1c5a"));
      List<Posts> posts = template.find(query, Posts.class);
      return posts;

  }

I tried it with this query, and it works, but returns only the specific record associated with the identifier:

query.addCriteria(Criteria.where("_id").is("55732dccf58c555b6d3f1c5a"));

I also tried with Basic Query and did this, and it also returns an empty array:

BasicQuery query1 = new BasicQuery("{'_id': {$lt:'ObjectId(\"55732dccf58c555b6d3f1c5a\")'}}).limit(5).sort({'_id':-1}");

I'm at a dead end. How can I return all documents under a specific Mongo ObjectID in the database?

+4
source share
2 answers

So, after searching for an hour, I found a solution - I had to see this post, which is not in java, but in node.js.

MongoDB query based on Mongo id in node.js application

, java, , , objectID lt. objectID .

      ObjectId objID = new ObjectId("55732dccf58c555b6d3f1c5a");
      query.addCriteria(Criteria.where("_id").lt(objID));
+5

. . , "lt" , .

, "lt" "gt" mongodb, , ASC DESC ObjectId. , , "lt" , reset ( , ). , : http://www.mkyong.com/mongodb/spring-data-mongodb-query-document/

Query nextStoryQuery = new Query(); //1
previousStoryQuery.addCriteria(Criteria.where("_id").lt(objID)).limit(1); //2
previousStoryQuery.with(new Sort(Sort.Direction.DESC, "_id")); //3

. 2 1 , . 3: , ASC DESC ObjectId ObjectId.

+2

All Articles