Johnny
Johnny

Reputation: 2910

How to do paging in Mongodb with node-mongodb-native cursor?

I am working on a REST API, I need to be able to page through MongoDB, from what I understand cursor is the best way to do it. I can get the cursor, but how to serialize it to send it to the client? and how would the server de-serialize it when it comes back, and query with it? Is this even possible?

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

Regards,

Upvotes: 0

Views: 1211

Answers (1)

yonran
yonran

Reputation: 19174

Hi I saw your question in the node-mongodb-native mailing list too. I know that you can serializing query cursors in App Engine but I don't think you can do the exact analogue in MongoDb. In Bigtable, the cursor that the client gets is the actual key of the last row that was scanned, whereas in MongoDb, the client's cursor is only a 64-bit number. Judging by the MongoDb documentation on cursor timeouts, it's not really advisable to keep around cursors per-user because each outstanding cursor takes up database memory.

But if you still want to use MongoDb cursors for some reason, I think it's possible to hack around with cursor.js but I haven't tried myself. To the client, a cursor is nothing more than a 64-bit cursorId, and you should be able to create a new Cursor to issue the correct OP_GETMORE requests to the server. I think it would look something like this (untested!):

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

Upvotes: 2

Related Questions