Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB count and results

I would really appreciate if someone could help me with something: I need to make a normal query to the database but, as my collection is very large (10000 documents) I need to do the query and use $limit and $skip. That part I solved but now I want to have a count to all the documents, even if the returned ones are less. The output should be something like this:

{
     count: 1150,
     data: [/*length of 50*/]
}

Could anyone help please? Thank you very much.

like image 388
Rares Golea Avatar asked Nov 17 '25 05:11

Rares Golea


1 Answers

Since you mentioned you are making a normal query, its not wise to go for aggregation. find() will be a much better option here. Instead you can use the find query itself. The commands to do this in mongoDB console is shown below:

> var docs = db.collection.find().skip(10).limit(50)
> docs.count()
189000
> docs.length()
50
like image 120
Sarath Nair Avatar answered Nov 18 '25 20:11

Sarath Nair



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!