Identify last document from MongoDB find() result set

后端 未结 3 1181
野性不改
野性不改 2020-12-29 07:27

I\'m trying to \'stream\' data from a node.js/MongoDB instance to the client using websockets. It is all working well.

But how to I identify the last document in t

3条回答
  •  难免孤独
    2020-12-29 08:04

    Use sort and limit, if you want to use cursor :

    var last = null;
    var findCursor = collection.find({}).cursor();
    findCursor.on("data", function(data) {
       last = data;
       ...
    });
    findCursor.on("end", function(data) {
       // last result in last
       ....
    }); 
    

提交回复
热议问题