Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get First 10 result in MongoDB collection

I want to get First top 10 result in MongoDb Query

For that I tried something like this

return _users.Collection.FindAll().Limit(10).ToList();

But this is not working then I tried following thing according to most of resources, but this one also not working

return _users.Collection.Aggregate([{ "$limit": 10 }]).ToList();
like image 708
kez Avatar asked Nov 11 '16 14:11

kez


People also ask

Which command helps get first 10 documents from MongoDB?

On the MongoDB shell you can do: db. collectionName.

How do we display only first 5 documents of a collection using MongoDB?

To skip records in MongoDB, use skip(). With that, to display only a specific number of records, use limit().

How do I get the oldest record in MongoDB?

Use the $first operator to get the first record of the group, which will also be the oldest. Use the $last operator to get the last record in the group, which will also be the newest. To get the entire record use the $$ROOT system variable.


2 Answers

Try

return _users.Collection.Find(x => true).Limit(10).ToList();

Instead of FindAll()

like image 173
pieperu Avatar answered Sep 22 '22 00:09

pieperu


  • find({}) will find all the documents and limit() will filter your results.

     return _users.Collection.find({}).limit(10).ToList();
    
like image 34
Taib Islam Dipu Avatar answered Sep 20 '22 00:09

Taib Islam Dipu