Skip to content

Instantly share code, notes, and snippets.

@yocontra
Last active December 14, 2015 05:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yocontra/5035768 to your computer and use it in GitHub Desktop.
Save yocontra/5035768 to your computer and use it in GitHub Desktop.
Streaming mongodb APIs
var mongoose = require('mongoose');
var http = require('http');
var JSONStream = require('JSONStream');
var PersonSchema = new mongoose.Schema({
name: String,
num: Number
});
mongoose.connect("mongodb://localhost:27017");
var Person = mongoose.model('Person', PersonSchema);
var startServer = function(){
var handler = function (req, res) {
var query = Person.find({name:'Eric'});
var stream = query.stream();
res.statusCode = 200;
stream.pipe(JSONStream.stringify())
.pipe(res);
};
http.createServer(handler).listen(8080);
console.log("Listening");
};
startServer();
/*
var docs = [];
for (var i = 0; i <= 50000; i++) {
console.log(i);
docs.push({name:'Eric', num: i});
}
Person.create(docs, function (err, me){
if (err) console.log(err);
startServer();
});
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment