Node.js连接mongo

时间:2019-07-18 06:35:04

标签: node.js mongodb

我尝试将节点应用程序中的数据存储移动到mongo db。但是我对mongo db有一个简单的问题。

我有一个按钮,在网站上单击,将呼叫/ datastore

app.post('/datastore', (req, res) => {

    client.connect(err => {
        var dbo = client.db("test");
        dbo.listCollections().toArray(function(err, items){
            if (err) throw err;

            console.log(items);
            if (items.length == 0)
                console.log("No collections in database")
        });

        client.close();
    });

});

第一次单击该按钮,效果很好。但是,如果我第二次单击该按钮,则会收到错误消息:

  

不支持选项[服务器],选项[caseTranslate]是   不支持的选项[dbName]不支持的选项   不支持[凭据]   /Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb/lib/utils.js:132         犯错         ^

     

MongoError:拓扑已被破坏       在initializeCursor(/Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb-core/lib/cursor.js:596:25)       在nextFunction(/Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb-core/lib/cursor.js:456:12)       在CommandCursor.Cursor.next(/Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb-core/lib/cursor.js:766:3)       在CommandCursor.Cursor._next(/Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb/lib/cursor.js:216:36)       在fetchDocs(/Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb/lib/operations/cursor_ops.js:217:12)       在toArray(/Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb/lib/operations/cursor_ops.js:247:3)       在executeOperation(/Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb/lib/utils.js:416:24)       在CommandCursor.Cursor.toArray(/Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb/lib/cursor.js:829:10)       在client.connect.err(/Users/ingofoerster/Downloads/development/testrunner/start.js:256:35)       结果(/Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb/lib/utils.js:410:17)       在executeCallback(/Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb/lib/utils.js:402:9)       在err(/Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb/lib/operations/mongo_client_ops.js:286:5)       在connectCallback(/Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb/lib/operations/mongo_client_ops.js:265:5)       在topology.connect(/Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb/lib/operations/mongo_client_ops.js:417:5)       在ReplSet.connectHandler(/Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb/lib/topologies/replset.js:343:9)       在Object.onceWrapper(events.js:281:20)       在ReplSet.emit(events.js:193:13)       在/Users/ingofoerster/Downloads/development/testrunner/node_modules/mongodb-core/lib/topologies/replset.js:786:18       在processTicksAndRejections(internal / process / task_queues.js:79:9)

我无法解释为什么会发生这种情况,因为我的代码中包含client.close()。知道为什么我不能多次调用该函数吗?

1 个答案:

答案 0 :(得分:1)

首次单击该按钮时,您可以按预期获得结果,但是获得结果后,您还可以通过调用来关闭连接 client.close();,这不允许MongoDB第二次重新连接。

理想情况下,对于每个API调用,您都应该重用现有的连接,而不是调用connect方法。

Connection pooling example is taken from here

var express = require('express');
var mongodb = require('mongodb');
var app = express();

var MongoClient = require('mongodb').MongoClient;
var db;

// Initialize connection once
MongoClient.connect("mongodb://localhost:27017/integration_test", function(err, database) {
  if(err) throw err;

  db = database;

  // Start the application after the database connection is ready
  app.listen(3000);
  console.log("Listening on port 3000");
});

// Reuse database object in request handlers
app.get("/", function(req, res) {
  db.collection("replicaset_mongo_client_collection").find({}, function(err, docs) {
    docs.each(function(err, doc) {
      if(doc) {
        console.log(doc);
      }
      else {
        res.end();
      }
    });
  });
});