gpt4 book ai didi

node.js - 使用限制和排序时MongoDB解析错误

转载 作者:太空宇宙 更新时间:2023-11-03 22:55:01 25 4
gpt4 key购买 nike

我只是尝试在 mongoDB 中使用排序和限制,但只有当我输入的限制太大时,才会出现解析错误。

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

当将 limit 设置为 10000 时,此代码有效,但是当我设置 limit=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)

这是什么意思以及如何解决它?

最佳答案

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

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

您可以阅读document about sort这里,它告诉你:

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.

此外,您还应该考虑@Neil Lunn 的建议。

关于node.js - 使用限制和排序时MongoDB解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21767673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com