Mayank
Mayank

Reputation: 134

Limit find using Monk in mongoDB

I have a large collection of documents. I want to get the first 100 of these. From the Monk Docs, this is the find method I am using

var documents = [];
users.find({}, function (err, docs){
  for(i=0;i<100;i++)
     documents.push(docs[i]);
});

This is highly wasteful since, the entire documents are anyway retrieved. I want something like this (from the mongodb docs)

docs =  db.users.find().limit( 100 );

I tried in monk,

users.find({}, function (err, docs){
  for(i=0;i<docs.length;i++)
     documents.push(docs[i]);
}).limit(100);

But it gives an error saying that there is no function limit in the "promise" object that is returned before it.

Is there such an option in Monk to limit the number of documents?

Upvotes: 3

Views: 1799

Answers (3)

us_david
us_david

Reputation: 4917

If you want add pagenumber and sorting, here is how to do it with monk:

users.find({}, {limit: 100, skip: pagenumber, sort: {'username': 1}})  

where limit is for page size, skip is page number, sorting result by username ascending.

Upvotes: 1

go-oleg
go-oleg

Reputation: 19480

Yes, you can pass it as an option in the second parameter:

users.find({}, { limit : 100 }, function (err, docs){
  for(i=0;i<docs.length;i++)
     documents.push(docs[i]);
});

This comes from the native node mongodb driver, which monk wraps via mongoskin:

http://mongodb.github.io/node-mongodb-native/markdown-docs/queries.html#query-options

Upvotes: 3

Leonid Beschastny
Leonid Beschastny

Reputation: 51480

You could pass options object as a second parameter to .find():

users.find({}, {limit: 100}, next);

Upvotes: 2

Related Questions