MongoDB parseError使用限制和sorting

我只是想在mongoDB中使用sorting和限制,但是我得到一个parseError,只有当我input的限制是很大的。

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)); )}; 

此代码工作时设置限制为10000,但是当我设置限制= 12000,我会得到以下错误:

 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) 

这是什么意思,我该如何解决呢?

如果限制数量是你的查询失败的关键原因,那么我build议你应该确保你在指定的sorting字段上有一个索引(在你的情况下它是“gameNum”)。 如果没有,build立一个索引:

 db.colname.ensureIndex({gameNum: 1}) 

你可以在这里阅读有关sort的文档 ,它告诉你:

注意对于不使用索引的内存中sorting,sort()操作明显较慢。 sort()操作将在使用32 MB内存时中止。

此外,你还应该考虑@尼尔·伦的build议。

我会考虑确保你有最新的驱动程序作为你做的第一件事,只是为了确保你没有find一个可能已经解决的问题。

我看到的主要观点是,你正在访问大量的logging,并使用toArray() ,这将是完全按照说,只是将所有的文件拉到一个数组。

我会尝试改变这个调用来使用每个()并在callback中推入你的数组。 至less在这种情况下,如果有什么东西导致它在特定的文档上发生变化,那么您将更好地了解所发生的事情。

考虑一下你的用例,10,000个文档是很多要返回到客户端。 你可能需要它,但如果有更好的方法来实现你想要的,那么你可能不会。