Reputation: 20574
I'm just trying to use sort and limit in mongoDB, but I get a parseError, only if the limit I enter is to big.
handCollection = db.collection('hands');
handCollection.count(function(err,res){numHands=res;console.log(numHands)}) //logs 12542
limit=10000
query= query.sort({gameNum:-1}).limit(limit)
query.toArray(function(er,hands){
if (er)
throw er
console.log('Hands Sent:'+hands.length)
res.send(JSON.stringify(hands));
)};
This code works when setting limit to 10000, but when I set limit=12000, I'll get the following error:
Error: parseError occured
at null.<anonymous> (C:\wamp\www\poker-tell\server\node_modules\mongodb\lib\mongodb\connec
tion\connection_pool.js:182:34)
at EventEmitter.emit (events.js:98:17)
at Socket.<anonymous> (C:\wamp\www\poker-tell\server\node_modules\mongodb\lib\mongodb\conn
ection\connection.js:389:20)
at Socket.EventEmitter.emit (events.js:95:17)
at Socket.<anonymous> (_stream_readable.js:746:14)
at Socket.EventEmitter.emit (events.js:92:17)
at emitReadable_ (_stream_readable.js:408:10)
at emitReadable (_stream_readable.js:404:5)
at readableAddChunk (_stream_readable.js:165:9)
at Socket.Readable.push (_stream_readable.js:127:10)
What does that mean and how can I solve it ?
Upvotes: 1
Views: 1421
Reputation: 4203
If limit quantity is the key reason why your query fails, then I suggest you should make sure if you have an index on the specified sort field (in your case it's "gameNum"). If not, build an index:
db.colname.ensureIndex({gameNum: 1})
you can read the document about sort here, which tells you:
Note For in-memory sorts that do not use an index, the sort() operation is significantly slower. The sort() operation will abort when it uses 32 megabytes of memory.
Besides, you should also consider @Neil Lunn's suggestions.
Upvotes: 2
Reputation: 151220
I would look at making sure you have the latest driver as the first thing you do, just to make sure you are not picking up a problem that may have been addressed.
The main point I see is that you are accessing a lot of records and using toArray(), which is going to do exactly what is says and just pull all the documents into an array.
I would try changing that call to use each() and push onto your array within the callback. At least that way if there is something that is causing this to trip up on a specific document, you'll have a better idea of where that is happening.
Consider your use case as well, 10,000 documents is a lot to return to a client. You may need it, but then again you may not if there is a better way to achieve what you want.
Upvotes: 2