node-mongodb-native:如何通过我的应用程序共享连接回调的db api对象 [英] node-mongodb-native: How can I share the db api object of the connection callback through my application

查看:79
本文介绍了node-mongodb-native:如何通过我的应用程序共享连接回调的db api对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在从猫鼬回滚到node-mongodb-native.

I am currently rolling back from mongoose to node-mongodb-native.

所以我在这个话题上很陌生.但是,我目前的问题是想要在服务器启动时创建数据库集合,然后可以在应用程序中使用它.不幸的是,我只能在存储库中找到一些示例,在这些示例中,您只能直接在connect函数的回调中执行数据库操作.

So I am quite new at this topic. However my issue currently is that want to create a database collection on server start which I then can use through the application. Unfortunately I only find examples in the repository where you only can do database actions directly in the callback of the connect function.

文档:

var mongodb = require("mongodb"),
    mongoServer = new mongodb.Server('localhost', 27017),
    dbConnector = new mongodb.Db('example', mongoServer);

db_connector.open(function(err, db) {
    if (err) throw new Error(err);

    // here I can do my queries etc.
});

但是当我处于某些路由回调中时,如何获得对回调中db对象的访问? 目前,我唯一的想法是将应用程序包装到回调中:

But how can I get access to the db object in the callback when I am in some route callback? Currently the only idea I would have is wrapping the application into the callback:

var mongodb = require("mongodb"),
    express = require("express"),
    mongoServer = new mongodb.Server('localhost', 27017),
    dbConnector = new mongodb.Db('example', mongoServer);

var app = new express();

db_connector.open(function(err, db) {
    if (err) throw new Error(err);

    app.get('/products', function(req, res, next) {
        db.collection('products', function(err, collection) {
            if (err) next(new Error(err));
            collection.find({}, function(err, products) {
                res.send(products);
            });
        });
    });

});

但是我不认为这是应该的方式?

But I do not think this is the way it should meant to be?

没有办法创建一个同步数据库连接调用,然后我可以轻松地在整个应用程序中使用猫鼬吗?

Isn't there the way to create a sync database connection call which I then can easily use through the whole application how it was by mongoose?

关于博多

推荐答案

Db.open打开与mongodb的连接并返回对其自身的引用.参见此处获取源代码: https://github.com/mongodb/node-mongodb-native/blob/master/lib/mongodb/db.js#L245

Db.open opens the connection to mongodb and returns a reference to itself. See here for the sourcecode: https://github.com/mongodb/node-mongodb-native/blob/master/lib/mongodb/db.js#L245

您要做的就是推迟启动Express应用程序,侦听其端口并接收请求,直到建立与mongodb的连接为止.

All you want is to hold off on starting your express app listening on it's port and receiving requests until your connection to mongodb has been established.

所以你可以做的是这个

var mongodb = require("mongodb"),
    express = require("express"),
    mongoServer = new mongodb.Server('localhost', 27017),
    dbConnector = new mongodb.Db('example', mongoServer),
    db;

var app = new express();

app.get('/products', function(req, res, next) {
  db.collection('products', function(err, collection) {
    if (err) next(new Error(err));
    collection.find({}, function(err, products) {
      res.send(products);
    });
  });
});

db_connector.open(function(err, opendb) {
  if (err) throw new Error(err);
  db = opendb;
  app.listen(3000);
});

我不确定这是否是个好主意.如果存在连接中断或重新启动mongodb进程的情况,则此解决方案不允许您重新创建连接.因此,尽管上述方法可行,但创建一个包装与mongodb的连接的包装的方法可能是一个更好的主意.

What I'm not sure about though is whether this is a good idea. This solution doesn't allow you to recreate your connection if there has been a connection break or you restarted your mongodb process. So while the above might work, it might be a better idea to create a method that will wrap the creation of a connection to mongodb.

这篇关于node-mongodb-native:如何通过我的应用程序共享连接回调的db api对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆