开发者

node-mongodb-native keep collection?

开发者 https://www.devze.com 2023-02-20 14:24 出处:网络
Right now I\'m opening a collection on every request: ie: app.get(\'/route\', function (req, res) { db.collection(\'user\', function (err, 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

精彩评论

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