[JAVA-792] Need a simpler method to get the last N results for a query Created: 26/Mar/13 Updated: 26/Mar/13 Resolved: 26/Mar/13 |
|
| Status: | Closed |
| Project: | Java Driver |
| Component/s: | API |
| Affects Version/s: | None |
| Fix Version/s: | None |
| Type: | Improvement | Priority: | Major - P3 |
| Reporter: | Greg Bowering | Assignee: | Unassigned |
| Resolution: | Won't Fix | Votes: | 1 |
| Labels: | query | ||
| Remaining Estimate: | Not Specified | ||
| Time Spent: | Not Specified | ||
| Original Estimate: | Not Specified | ||
| Description |
|
DBCursor.limit(10) is a simple API that allows the user to get the first 10 (or no more than 10) documents back from the DB. However there does not seem to be a simple method to get the last 10 rows. Normally (in SQL land) you would do something like DBCursor.limit(-10) to fetch the last 10 documents, however it appears this is currently used to support backwards compatibility with some old behavior. Perhaps a new API like DBCursor.last(10) could be introduced for this? Currently the user needs to code something like the following to get the last 10 documents from a query:
|
| Comments |
| Comment by Jeffrey Yemin [ 26/Mar/13 ] |
|
The driver is not going to do this unless the server supports it as a native operation. I suggest you use the reverse sort technique, since as you noticed the performance is much better (and the difference between the two will grow with the size of your collection). |
| Comment by Greg Bowering [ 26/Mar/13 ] |
|
I am using Mongo to store application logs aggregated via Flume-NG and Flume-MongoDB plugin, so documents are never updated and are usually retrieved in the "tail" in natural (insertion) order (e.g. 10 million records and we want to see the last 100 in natural order). I tried sort($natural:-1) combined with limit(10) and this performs about 30% faster than skip(count() - 10), the only difference in results being they were in reverse sort order, which is not a big problem unless you need to iterate over a large number in natural order without fetching all from the database first. Since DBCursor only goes in one direction, fetching the result set in reverse order might be problematic. Something like the proposed last(10) or limit(-10) would fetch the last 10 records in whatever the current sort order was (whether natural or specified). |
| Comment by Jeffrey Yemin [ 26/Mar/13 ] |
|
Typically if you are specifying limit, you are also specifying s sort criteria, in which case you can reverse the criteria to get the behavior you're looking for. In the case from the description, the sort order is not defined, so it's not clear to me how getting the first or last 10 makes a difference. See http://docs.mongodb.org/manual/reference/glossary/#term-natural-order. |
| Comment by Adeyemi Adegbile [ 26/Mar/13 ] |
|
Why not build a facade layer that calls the Mongo Server for to expose this functionality to all user of the API giving a consistency optimal usage. |