1

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().

Community
  • 1
  • 1
Melissa
  • 1,236
  • 5
  • 12
  • 29

2 Answers2

2

Ok, MongoDB is a document-centric repository storing JSON documents (really BSON but let's keep it simple for now). And, a Heap is a form of specialized tree. My assumption is you will model your heap(s) as a JSON document and have to code the insertion and retrieval methods in JavaScript. I am going to play around with this some more, but perhaps the research will help you to get farther.

So, here are two great pieces of research to help you:

  1. Modeling a tree in a document database here.
  2. Storing a directory hierarchy in a Key Value store here. (Yes, I know MongoDB is technically not a KV store, but there is good information in the post...)
Community
  • 1
  • 1
jsh
  • 338
  • 1
  • 8
  • Ok, this may be interesting as well...https://github.com/thauburger/js-heap/blob/master/package.json – jsh Oct 05 '15 at 03:00
  • How's https://github.com/thauburger/js-heap better than https://www.npmjs.com/package/heap? – Melissa Oct 05 '15 at 03:20
  • I can't say that it is. Only commenting on additional research/findings. I plan on trying both to see which one may be better ... – jsh Oct 05 '15 at 03:26
0

After doing a bit more research, I have decided to just put a complex index in my Mongoose schema and then use MongoDB's find() and sort(). See this for more details.

Community
  • 1
  • 1
Melissa
  • 1,236
  • 5
  • 12
  • 29