开发者

node-mongodb-native keep collection?

Right now I'm opening a collection on every request:

ie:

  app.get('/route', function (req, res) {
    db.collection('user', function (err, collection) {
      collection.find(blah) // do something

  app.get('/route2', function (req, res) {
    db.collection('user', function (err, collection) {
      collection.find(foo) // do something

  app.get('/route3', function (req, res) {
    db.collection('user', function (err, collection) {
      collection.find(bar) // do something

Is that开发者_如何学Python incorrect? I'm thinking I should save the 'user' collection to a variable and not get it every request.

Thanks.


You can have a variable collection and use it:

  db.collection('user', function (err, collection) {
    app.get('/route', function (req, res) {
      collection.find(blah) // do something
    }
    app.get('/route2', function (req, res) {
      collection.find(foo) // do something
    }
    app.get('/route3', function (req, res) {
      collection.find(bar) // do something
    }
  }

Or you can use some of the modules that simplify those operations (Mongoose, Mongolia ...)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜