Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limiting Query result in MongoDB

I have 20,000+ documents in my mongodb. I just learnt that you cannot query them all in one go.

So my question is this:

I want to get my document using find(query) then limit its results for 3 documents only and I can choose where those documents start from.

For example if my find() query resulted in 8 documents :

[{doc1}, {doc2}, {doc3}, {doc4}, {doc5}, {doc6}, {doc7}, {doc 8}]

command limit(2, 3) will gives [doc3, doc4, doc5]

And I also need to get total count for all that result(without limit) for example : length() will give 8 (the number of total document resulted from find() function)

Any suggestion? Thanks

like image 320
DennyHiu Avatar asked Dec 27 '15 16:12

DennyHiu


1 Answers

add .skip(2).limit(3) to the end of your query

like image 160
Martins Untals Avatar answered Sep 18 '22 17:09

Martins Untals