Mongotemplate - 根据大于(gt)或小于(lt)运算符查询ObjectId
Mongotemplate - Query ObjectId according to greater than (gt) or less than (lt) operator
当我将其输入我的控制台时,它起作用了:
db.posts.find({"_id": {$lt:ObjectId("55732dccf58c555b6d3f1c5a")}}).limit(5).sort({"_id":-1})
当我使用 mongotemplate 时,它不起作用并且return是一个空白数组:
@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;
}
我用这个查询试了一下,它有效,但只有 returns 与 id 相关的特定条目:
query.addCriteria(Criteria.where("_id").is("55732dccf58c555b6d3f1c5a"));
我也尝试使用 Basic Query 并执行了此操作,它也是 return 一个空白数组:
BasicQuery query1 = new BasicQuery("{'_id': {$lt:'ObjectId(\"55732dccf58c555b6d3f1c5a\")'}}).limit(5).sort({'_id':-1}");
我很难过。我如何 return 数据库中某个 Mongo ObjectID 下面的所有文档?
因此,在搜索了一个小时后,我找到了解决方案 - 我不得不查看这个 post,它不在 java 中,而是在 node.js 中。
Querying a MongoDB based on Mongo ID in a node.js app
谢天谢地,该语言接近于 java,所以我看到您无法通过仅将 objectID 插入 lt 运算符来进行查询。您必须创建一个 objectID 对象并将其插入到运算符中。
ObjectId objID = new ObjectId("55732dccf58c555b6d3f1c5a");
query.addCriteria(Criteria.where("_id").lt(objID));
This post was a life saver. Spent hours searching for this. I wanted to add that the answer above does the "lt" comparison but doesn't show the sorting part.
What I discovered was when you do a "lt" or "gt" comparison in mongodb, it is not guaranteed that you will get the immediate next ASC or DESC ObjectId. In fact, I noticed that when I got to the end of my collection and did an "lt", it reset and gave me the very first document in my collection (instead of the one just before it). For that, I have to add to the above solution which I found here: 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
Note, on line 2, I was just looking for 1 result so I added the limit function.
Line 3: By adding the Sort functionality, it guarantees the immediate ASC or DESC ObjectId value based on the current ObjectId value.
当我将其输入我的控制台时,它起作用了:
db.posts.find({"_id": {$lt:ObjectId("55732dccf58c555b6d3f1c5a")}}).limit(5).sort({"_id":-1})
当我使用 mongotemplate 时,它不起作用并且return是一个空白数组:
@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;
}
我用这个查询试了一下,它有效,但只有 returns 与 id 相关的特定条目:
query.addCriteria(Criteria.where("_id").is("55732dccf58c555b6d3f1c5a"));
我也尝试使用 Basic Query 并执行了此操作,它也是 return 一个空白数组:
BasicQuery query1 = new BasicQuery("{'_id': {$lt:'ObjectId(\"55732dccf58c555b6d3f1c5a\")'}}).limit(5).sort({'_id':-1}");
我很难过。我如何 return 数据库中某个 Mongo ObjectID 下面的所有文档?
因此,在搜索了一个小时后,我找到了解决方案 - 我不得不查看这个 post,它不在 java 中,而是在 node.js 中。
Querying a MongoDB based on Mongo ID in a node.js app
谢天谢地,该语言接近于 java,所以我看到您无法通过仅将 objectID 插入 lt 运算符来进行查询。您必须创建一个 objectID 对象并将其插入到运算符中。
ObjectId objID = new ObjectId("55732dccf58c555b6d3f1c5a");
query.addCriteria(Criteria.where("_id").lt(objID));
This post was a life saver. Spent hours searching for this. I wanted to add that the answer above does the "lt" comparison but doesn't show the sorting part.
What I discovered was when you do a "lt" or "gt" comparison in mongodb, it is not guaranteed that you will get the immediate next ASC or DESC ObjectId. In fact, I noticed that when I got to the end of my collection and did an "lt", it reset and gave me the very first document in my collection (instead of the one just before it). For that, I have to add to the above solution which I found here: 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
Note, on line 2, I was just looking for 1 result so I added the limit function. Line 3: By adding the Sort functionality, it guarantees the immediate ASC or DESC ObjectId value based on the current ObjectId value.