在连接回调之外访问MongoDB

时间:2021-10-07 12:36:13

I know that similar questions have been asked, but no one actually shows code that does this, they only link to pages that also don't show code.

我知道已经提出了类似的问题,但实际上没有人显示执行此操作的代码,它们只链接到也不显示代码的页面。

Anyway, basically my node server receives data on a socket.io event. I want that data to go into MongoDB. The problem is that all the code examples I've seen for mongo only manipulate the db inside the MongoClient.connect callback, using the db object.

无论如何,基本上我的节点服务器接收socket.io事件的数据。我希望这些数据进入MongoDB。问题是我在mongo上看到的所有代码示例都只使用db对象操作MongoClient.connect回调中的db。

Since, I will be getting a lot of this data, I don't want to initialize over and over again.

因为,我将获得大量这些数据,我不想一遍又一遍地初始化。

What I need is effectively this:

我需要的是这样的:

MongoClient.connect(("mongodb://" + process.env.IP + ":27017/feedback"),
function(err, db) { ... });

And then later:

然后呢:

socket.on('data', function (data) {
    db.doStuff();
});

2 个解决方案

#1


6  

MongoClient.connect() return a promise if you don't give it a callback, you can declare a global promise :

MongoClient.connect()如果你不给它一个回调就返回一个promise,你可以声明一个全局的promise:

var connect = MongoClient.connect(url);

socket.on('data', function(data) {
  connect.then(function(db) {

  });
});

socket.on('otherData', function(data) {
  connect.then(function(db) {

  });
});

#2


1  

You should be able to load the connection into a var and just use that instead of re-establishing the connection for every query.

您应该能够将连接加载到var中,只需使用它而不是为每个查询重新建立连接。

I use a similar approach myself, locating the connection code into a its own module, something like so:

我自己使用类似的方法,将连接代码定位到自己的模块中,如下所示:

NOTE: This code is off-the-cuff, untested, and I'm a little drunk.

注意:此代码是袖手旁观,未经测试,我有点醉了。

connect.js

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

module.exports = function(params) {

  var ip = params.ip || process.env.IP;
  var port = params.port || 27017;
  var collection = params.collection;

  var db = MongoClient.connect('mongodb://' + ip + ':' + port + '/' + collection);

  return db;

}



Then in any given other module in your app, you would require connection.js and pass the params for any given connection, like so:

然后在应用程序的任何给定的其他模块中,您将需要connection.js并传递任何给定连接的参数,如下所示:

onFeedback.js

var feedbackDB = require('./connection.js')({
  collection : 'feedback'
});

socket.on('data', function (data) {
  feedbackDB(function(db){
    db.doStuff();
  };
});

#1


6  

MongoClient.connect() return a promise if you don't give it a callback, you can declare a global promise :

MongoClient.connect()如果你不给它一个回调就返回一个promise,你可以声明一个全局的promise:

var connect = MongoClient.connect(url);

socket.on('data', function(data) {
  connect.then(function(db) {

  });
});

socket.on('otherData', function(data) {
  connect.then(function(db) {

  });
});

#2


1  

You should be able to load the connection into a var and just use that instead of re-establishing the connection for every query.

您应该能够将连接加载到var中,只需使用它而不是为每个查询重新建立连接。

I use a similar approach myself, locating the connection code into a its own module, something like so:

我自己使用类似的方法,将连接代码定位到自己的模块中,如下所示:

NOTE: This code is off-the-cuff, untested, and I'm a little drunk.

注意:此代码是袖手旁观,未经测试,我有点醉了。

connect.js

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

module.exports = function(params) {

  var ip = params.ip || process.env.IP;
  var port = params.port || 27017;
  var collection = params.collection;

  var db = MongoClient.connect('mongodb://' + ip + ':' + port + '/' + collection);

  return db;

}



Then in any given other module in your app, you would require connection.js and pass the params for any given connection, like so:

然后在应用程序的任何给定的其他模块中,您将需要connection.js并传递任何给定连接的参数,如下所示:

onFeedback.js

var feedbackDB = require('./connection.js')({
  collection : 'feedback'
});

socket.on('data', function (data) {
  feedbackDB(function(db){
    db.doStuff();
  };
});