3 ms·
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 the
by impostervt 14y ago
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.
- anton-107 14y agodo you keep them updated? or are they read-only collections? also some collections could be just a waste of ram for your app
- joshguthrie 14y agoEDIT: 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...
- bellwether 14y agoi 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.
- bellwether 14y agoyour 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).