Given a LARGE (hundreds of thousands) collection of event documents (see below for example), what is the most performant method to retrieve the first event with an _id greater than (n) ?
Example Document
{
_id: NumberLong(352757), // Uniqueness guaranteed
type: "BallDropped",
createdAt: "2014-01-01T00:00:00Z",
// ... followed by dynamic properties of unknown size
}
Current Implementation
Given a collection of many events, retrieve the first event with an _id greater than 35.
First, retrieve the id of the event using aggregate.
I do this assuming that the projection phase (return just the id) will be more performant than cycling over full documents of unknown size.
db.events.aggregate(
{ $project: { _id: 1 } },
{ $match: { _id: { $gt: NumberLong(35) } } },
{ $sort: { _id: 1 } },
{ $limit: 1 }
)
Then, I call findOne with the returned _id to retrieve that document.
What are your thoughts?