News for hackers. Stuff that matters.
Re:MongoDB singleton connection in Node.Js by impostervt on Friday April 05, 2013 @07:36AM (#5497727)
I like to initialize the connection to the database in my app.js, and create a global variable to each of my app's collections. The collection variables are then always usable throughout the rest of the app, so I don't have to connect to the database or find the collection again.

Is there any reason to not do it my way? I never see it shown that way in examples so I wonder if I'm missing something.

Re:MongoDB singleton connection in Node.Js by bellwether on Friday April 05, 2013 @09:01AM (#5498096)
your approach makes sense and is similar to what the author is doing here. but it is easier to maintain and extend as one object with properties for each collection (as joshguthrie suggested).
Re:MongoDB singleton connection in Node.Js by anton-107 on Friday April 05, 2013 @08:47AM (#5498028)
do you keep them updated? or are they read-only collections?

also some collections could be just a waste of ram for your app

Re:MongoDB singleton connection in Node.Js by joshguthrie on Friday April 05, 2013 @08:57AM (#5498080)
EDIT: As bellwether pointed out, in MongoDb, loading a collection only means something roughly similar to "pre-allocating the connection and meta-information": when querying a collection, no "actual" data is transmitted from server to client.

As strange as it may sound...there's no need to update them (or my apps have been lucking it out for the last six months?).

When you use less than 20 collections, this is not the kind of thing that'll become a "waste of ram". You could try a LRU to remove unused collections and get them later...but you would still lose more RAM because of the LRU manager...

Re:MongoDB singleton connection in Node.Js by bellwether on Friday April 05, 2013 @09:05AM (#5498119)
i think anton assumed the data for the collections was being read into memory. this aoproach would just be pre-allocating the connection and meta information. the collections would still be queried for data.