I'm using the MEAN stack for my web application which contains data that can be most efficiently stored and accessed as a heap structure. My application will have many calls (about a few hundred per minute) to retrieve the minimum element in a collection and work with it independently each time. Since I would like to use Node.js and MongoDB (hence the MEAN stack), I'm wondering how to build the data as a heap.
Ideally I would like to incorporate npm's heap somehow into the MEAN stack's MongoDB and/or Mongoose. But I'm not sure where and how.
Yes I could use MongDB's sort(), but that would mean I have to sort the collection every single time, based on what this answer says
db.the_collection.find().sort({the_field: 1}).limit(1)
Sorting basically the same thing hundreds of times per minute would be bad, wouldn't it? Or is MongoDB so very efficient that it can handle this?
Someone also mentioned $min. I don't know if it'll help as I don't even know how it works compared to sort()
.