Created
October 11, 2011 20:32
-
-
Save nicokaiser/1279333 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongodb = require('mongodb'); | |
// This example selects all documents of a collection and checks if they should be displayed. | |
// 15 documents should be displayed, but I have to select all from the DB because maybe | |
// myCheckFunction returns true only for the last 15 elements selected (if any). | |
// So at some point (if I have seen enough documents) the DB can stop sending me documents, | |
// i.e. not fetch another batch of 30 documents in this case. | |
var db = new mongodb.Db('mydb', new mongodb.Server('localhost', 27017, {})); | |
db.open(function(err, db) { | |
db.collection('mycollection', function(err, collection) { | |
var count = 0; | |
var stream = collection.find({}).streamRecords({fetchSize: 30}); | |
stream.on('end', function() { | |
db.close(); | |
}); | |
stream.on('data', function(data) { | |
// "myCheckFunction" checks if this document should be displayed or not | |
// this may be something I cannot do in MongoDB. | |
if (myCheckFunction(data)) { | |
count++; | |
doDisplay(data); // ... | |
if (count > 15) { // seen enough | |
stream.destroy(); // not implemented! | |
} | |
} | |
}); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment