Identify last document from MongoDB find() result set

后端 未结 3 1178
野性不改
野性不改 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:06

    Say I have companies collection. Below snippet gives me last document in the collection.

    db.companies.find({},{"_id":1}).skip(db.companies.find().count()-1);

    Code cannot rely on _id as it may not be on a specific pattern always if it's a user defined value.

提交回复
热议问题