-1

i'm new to NodeJS and i'm using MongoJS to connect to my database.

All is working fine, but when i use db.collection.find() function, it returns only the first 10 results on the database.

function listMessages(from,to){
console.log("[REQ] User "+from+" asked chat with "+to);
var htext = 'Welcome to the chat<br/>';
db.messages2.find({$or:[{from_id: from, to_id: to}, {from_id: to, to_id: from}]},function(err, echoData) { 
  if( err || !echoData) console.log("No messages found");
  else echoData.forEach( function(returnData) {
  htext += returnData.from_id+": "+returnData.text+"<br/>";
  });
  io.sockets.emit('html message '+from, htext, to);
});
}

It works fine, but returns only the first 10 results on the database, even if i use limit.
I've tried to use this too, and it didnt worked:

db.messages2.find({$or:[{from_id: from, to_id: to}, {from_id: to, to_id: from}]},{},{limit: 50}, function(err, echoData) {

What I need to do to fix this? Thanks!

1 Answers1

3

default limit in mongoDB is 10

from the documentation:

collection.find(query[[[, fields], options], callback]);

options - defines extra logic (sorting options, paging etc.)

Paging Paging can be achieved with option parameters limit and skip

so with a new limit:

db.messages2.find({}, {}, {limit: 1000}, function(err, echoData) {
Scott Jungwirth
  • 6,105
  • 3
  • 38
  • 35