I have a collection of posts (see the pseudo-json 🙂 ) :
[
{"title":"First","created":"2012-10-04 01:00:00"},
{"title":"Second","created":"2012-10-04 03:30:00"},
{"title":"Third","created":"2012-10-02 02:00:00"}
]
I am in the “2012-10-04” posts page and I want to know if there are older posts, just for displaying a “next page” button.
I can think about a query like this:
db.posts.find({"created":{$lt:ISODate("2012-10-04")} }).limit(1);
I still have to try it, but I think it will work… actually my question is:
What about its performance? Will it run at the same speed with billions of documents in the collection?
Provided there’s an index on “created” that should scale relatively well assuming you will shard your billions of collections as needed. Regardless of what kind of queries you do they will take longer on a larger data set, index or not. It’s just that the performance profile of b-tree walks is significantly better than raw table scans.
Your query is an open ended range query which is actually ideal in your case since it can return if it finds ANY document older than the specified date.
In short, yes, this will perform adequately and will scale with sharding.