我在一个REST API上工作,我需要能够通过MongoDB分页,从我所理解的游标是最好的方式来做它。我可以获取游标,但如何序列化它以将其发送到客户端?当它返回时,服务器将如何反序列化它,并使用它进行查询?这有可能吗?
collection.find({}, function(err, cursor) {
if (err) { console.log("Error in find: " + err); return;}
cursor.each(function(err, item) {
if (err) { throw err;
}
if (!item) {
console.log("All done");
} else {
console.log(sys.inspect(item));
}
});
});致以敬意,
发布于 2011-08-20 05:10:35
嗨,我也看到your question in the node-mongodb-native mailing list了。我知道你可以用serializing query cursors in App Engine,但我不认为你能用MongoDb做精确的模拟。在Bigtable中,客户端获得的游标是所扫描的最后一行的实际键,而在MongoDb中,客户端的游标只是一个64位数字。从MongoDb documentation on cursor timeouts判断,每个用户都保留游标是不可取的,因为每个未完成的游标都会占用数据库内存。
但是如果您出于某种原因仍然想使用MongoDb游标,我认为可以使用cursor.js,但我自己还没有尝试过。对于客户机来说,游标只不过是一个64位的cursorId,您应该能够创建一个新的游标来发出正确的OP_GETMORE requests to the server。我认为它看起来像这样(未经测试!):
var cursorId = cursor.cursorId;
// ...
var cursor2 = new mongodb.Cursor(db, collection).limit(100);
cursor2.state = mongodb.Cursor.OPEN;
cursor2.cursorId = cursorId;
cursor2.toArray(function(err, results) {console.log(results);});https://stackoverflow.com/questions/7125848
复制相似问题