So, I have data in the following model in MongoDB
:
- The default
_id
- A distint
uniq_id
(there will be multiple documents for eachuniq_id
) - A suitable timestamp value, ts for the document
Now, somewhere in my code, I wish to extract the oldest 100 documents for a given uniq_id
. So I run this query:
db.students.find({uniq_id:"xyz"}).limit(100).sort({ts:1})
My questions now are:
How does
limit()
work? Will it use the default_id
to get the first 100 documents? In other words, if I have 1100 documents resulting fromfind()
, I knowlimit(100)
is able to extract documents 1-100. Does it do this by making use of the default_id
index?To sort it based on timestamp (again to emphasize that I want the oldest 100 documents), do I need to set a compound index on both
uniq_id
and timestamp or is it better to create two single indexes onuniq_id
and timestamp separately?
I am of the assumption that two single field indexes will do the job. Can someone help me out with this?
Thanks in advance.