How to stream MongoDB Query Results with nodejs?

前端 未结 5 722
名媛妹妹
名媛妹妹 2020-12-23 14:33

I have been searching for an example of how I can stream the result of a MongoDB query to a nodejs client. All solutions I have found so far seem to read the query result at

5条回答
  •  半阙折子戏
    2020-12-23 14:55

    I have been studying mongodb streams myself, while I do not have the entire answer you are looking for, I do have part of it. you can setup a socket.io stream

    this is using javascript socket.io and socket.io-streaming available at NPM also mongodb for the database because using a 40 year old database that has issues is incorrect, time to modernize also the 40 year old db is SQL and SQL doesn't do streams to my knowledge

    So although you only asked about data going from server to client, I also want to get client to server in my answer because I can NEVER find it anywhere when I search and I wanted to setup one place with both the send and receive elements via stream so everyone could get the hang of it quickly.

    client side sending data to server via streaming

    stream = ss.createStream();
    blobstream=ss.createBlobReadStream(data);
    blobstream.pipe(stream);
    ss(socket).emit('data.stream',stream,{},function(err,successful_db_insert_id){
     //if you get back the id it went into the db and everything worked
    });
    

    server receiving stream from the client side and then replying when done

    ss(socket).on('data.stream.out',function(stream,o,c){
     buffer=[];
     stream.on('data',function(chunk){buffer.push(chunk);});
     stream.on('end',function(){
      buffer=Buffer.concat(buffer);
      db.insert(buffer,function(err,res){
       res=insertedId[0];
       c(null,res);
      });
     });
    });
    

    //This is the other half of that the fetching of data and streaming to the client

    client side requesting and receiving stream data from server

    stream=ss.createStream();
    binarystring='';
    stream.on('data',function(chunk){ 
     for(var I=0;i

    server side replying to request for streaming data

    ss(socket).on('data.stream.get',function(stream,o,c){
     stream.on('end',function(){
      c(null,true);
     });
     db.find().stream().pipe(stream);
    });
    

    The very last one there is the only one where I am kind of just pulling it out of my butt because I have not yet tried it, but that should work. I actually do something similar but I write the file to the hard drive then use fs.createReadStream to stream it to the client. So not sure if 100% but from what I read it should be, I'll get back to you once I test it.

    P.s. anyone want to bug me about my colloquial way of talking, I'm Canadian, and I love saying "eh" come at me with your hugs and hits bros/sis' :D

提交回复
热议问题