• <bdo id='DVVzK'></bdo><ul id='DVVzK'></ul>

    <i id='DVVzK'><tr id='DVVzK'><dt id='DVVzK'><q id='DVVzK'><span id='DVVzK'><b id='DVVzK'><form id='DVVzK'><ins id='DVVzK'></ins><ul id='DVVzK'></ul><sub id='DVVzK'></sub></form><legend id='DVVzK'></legend><bdo id='DVVzK'><pre id='DVVzK'><center id='DVVzK'></center></pre></bdo></b><th id='DVVzK'></th></span></q></dt></tr></i><div id='DVVzK'><tfoot id='DVVzK'></tfoot><dl id='DVVzK'><fieldset id='DVVzK'></fieldset></dl></div>
  • <legend id='DVVzK'><style id='DVVzK'><dir id='DVVzK'><q id='DVVzK'></q></dir></style></legend>
      <tfoot id='DVVzK'></tfoot>

        <small id='DVVzK'></small><noframes id='DVVzK'>

        如何通过 Promise 重用 mongodb 连接

        时间:2023-08-08
        <i id='5Js1J'><tr id='5Js1J'><dt id='5Js1J'><q id='5Js1J'><span id='5Js1J'><b id='5Js1J'><form id='5Js1J'><ins id='5Js1J'></ins><ul id='5Js1J'></ul><sub id='5Js1J'></sub></form><legend id='5Js1J'></legend><bdo id='5Js1J'><pre id='5Js1J'><center id='5Js1J'></center></pre></bdo></b><th id='5Js1J'></th></span></q></dt></tr></i><div id='5Js1J'><tfoot id='5Js1J'></tfoot><dl id='5Js1J'><fieldset id='5Js1J'></fieldset></dl></div>

          <small id='5Js1J'></small><noframes id='5Js1J'>

            <bdo id='5Js1J'></bdo><ul id='5Js1J'></ul>

              <tfoot id='5Js1J'></tfoot>

                  <tbody id='5Js1J'></tbody>
                • <legend id='5Js1J'><style id='5Js1J'><dir id='5Js1J'><q id='5Js1J'></q></dir></style></legend>
                  本文介绍了如何通过 Promise 重用 mongodb 连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想重用 MongoDB 连接.我知道 如何在 node.js 中重用 mongodb 连接我想使用 Promises 和 Mongo 驱动程序 v2 实现同样的目标

                  I want to reuse MongoDB connection. I 'am aware of How to reuse mongodb connection in node.js I want to acheive the same using Promises and Mongo driver v2

                  目前我必须为每个请求连接到数据库,这使得它变慢了.这是我的代码

                  Currently I have to connect to db for every request which makes it slow. This is my code

                  "use strict"
                  var app = require('./utils/express')();
                  var mongodb = require('mongodb');
                  
                  var MongoClient = mongodb.MongoClient;
                  //Actually I 'am connecting to MongoLab
                  var url = 'mongodb://localhost/my-mongo';
                  
                  app.set('port', (process.env.PORT || 5000));
                  
                  app.listen(app.get('port'), function () {
                    console.log('ParkMe app is running on port', app.get('port'));
                  });
                  
                  
                  app.get('/location/create', function(req,res,next){
                    MongoClient.connect(url).then(function(db) {
                      return db.collection('parkme_parkingLots').find({}).toArray().then(function (docs) {
                        return docs;
                      });
                    });
                  });
                  

                  我想做这样的事情:

                  "use strict"
                  var app = require('./utils/express')();
                  var mongodb = require('mongodb');
                  
                  var MongoClient = mongodb.MongoClient;
                  var url = 'mongodb://nidhind:1234@ds051635.mongolab.com:51635/my-mongo';
                  var db = MongoClient.connect(url).then(function(db) {
                      return db;
                  });
                  
                  app.set('port', (process.env.PORT || 5000));
                  
                  app.listen(app.get('port'), function () {
                    console.log('ParkMe app is running on port', app.get('port'));
                  });
                  
                  
                  app.get('/location/create', function(req,res,next){
                    db.collection('parkme_parkingLots').find({}).toArray().then(function (docs) {
                      return docs;
                    });
                  });
                  

                  推荐答案

                  你快到了,你的代码只需要做几处更改:

                  You're almost there, there are only a couple of changes in your code to be made:

                  "use strict"
                  var app = require('./utils/express')();
                  var mongodb = require('mongodb');
                  
                  var MongoClient = mongodb.MongoClient;
                  var url = 'mongodb://nidhind:1234@ds051635.mongolab.com:51635/my-mongo';
                  // no need to call then() yet
                  var connection = MongoClient.connect(url);
                  
                  app.set('port', (process.env.PORT || 5000));
                  
                  app.listen(app.get('port'), function() {
                    console.log('ParkMe app is running on port', app.get('port'));
                  });
                  
                  
                  app.get('/location/create', function(req, res, next) {
                    // the connection is opened once, use it at will
                    connection.then(function(db) {
                      db.collection('parkme_parkingLots').find({}).toArray().then(function(docs) {
                        return docs;
                      });
                    });
                  });
                  

                  这篇关于如何通过 Promise 重用 mongodb 连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在mongodb中只返回一个字段的值 下一篇:Firebase 警告:使用未指定的索引

                  相关文章

                  最新文章

                • <small id='yj0GP'></small><noframes id='yj0GP'>

                    <bdo id='yj0GP'></bdo><ul id='yj0GP'></ul>

                    <legend id='yj0GP'><style id='yj0GP'><dir id='yj0GP'><q id='yj0GP'></q></dir></style></legend>
                    <i id='yj0GP'><tr id='yj0GP'><dt id='yj0GP'><q id='yj0GP'><span id='yj0GP'><b id='yj0GP'><form id='yj0GP'><ins id='yj0GP'></ins><ul id='yj0GP'></ul><sub id='yj0GP'></sub></form><legend id='yj0GP'></legend><bdo id='yj0GP'><pre id='yj0GP'><center id='yj0GP'></center></pre></bdo></b><th id='yj0GP'></th></span></q></dt></tr></i><div id='yj0GP'><tfoot id='yj0GP'></tfoot><dl id='yj0GP'><fieldset id='yj0GP'></fieldset></dl></div>
                    1. <tfoot id='yj0GP'></tfoot>