在nodejs中连接后如何获取mongodb数据库的基本名称?

时间:2018-07-06 03:52:23

标签: mongodb

我发现最新版本的MongoDB nodejs驱动程序引入了MongoClient类,这是建立连接后我可以获得的第一个类实例。但是它没有提供默认的数据库实例。下面是源代码。

MongoClient.connect(url, (err, client) => {
    if(err) {
      return null;
    }
    client.db('test');  // how can I know the database name? Do I need to parse the url?
  });

上面的代码显示了连接后如何获取mongo客户端实例。我需要调用client.db来获取数据库实例。我的问题是我如何知道client实例中的默认数据库名称。我得到的只是连接url。我是否需要解析连接URL才能获得上面示例中的test连接数据库?

我知道有一种方法db.getName()返回数据库名称。但是,如何在不解析URL的情况下获取db实例以从连接中获取数据库名称?

6 个答案:

答案 0 :(得分:3)

当前mongodb驱动程序中。 Db类实例具有databaseName属性。因此,获取MongoClient已初始化的数据库名称很简单

const db = mongoClient.db();
const dbName = db.databaseName;

Nodejs driver Db documentation

答案 1 :(得分:0)

您要查找的可能是db.getName(),它返回当前数据库名称(默认)

答案 2 :(得分:0)

如下更改代码

let db = null;
MongoClient.connect('mongodb://localhost:27017/test', (err, client) => {
    if(err) {
      return null;
    }
    db = client.db('test');
  });
  

使用db.getName();

或如下更改配置

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

    var mongoClient = new MongoClient(new Server('localhost', 27017));
    mongoClient.open(function(err, mongoClient) {
      var db1 = mongoClient.db("mydb");

      mongoClient.close();
    });

refer to documentation

答案 3 :(得分:0)

数据库名称存在于从连接返回的client中:

db = client.db(client.s.options.dbName);

已在3.2版中验证

我在文档中找不到任何相关内容,因此不确定是否可以从任何版本的MongoDB驱动程序中访问它,或者在将来的版本中是否会更改它。

答案 4 :(得分:0)

我正在使用以下内容:

var paper;
var paperWidth, paperHeight = 0;
var xPos, yPos, radius = 0;
init();

function init() {
  paper = Snap("#svgContainer");
for (i = 0; i < data.length; i++) {
    data[i].circle = paper.circle(0, 0, 1); 

  }
function showJahresZahlen() {
    for (var i = 0; i < data.length - 0; i++) {
      xPos = map(data[i].longitude, (0 - 180), 180, 0, paperWidth);
      yPos = paperHeight - map(data[i].latitude, (0 - 90), 90, 0, paperHeight);

//first approach
circleColor = lava;
      function lava() {
        if (data.lastEruption < 100) {
          circleColor = "blue"
        } else if (data.lastEruption > 1000 && data.lastEruption < 1500) {
          circleColor = "red"
       } else {

          circleColor = "orange"
        }
      }
   data[i].circle.animate({

        cx: xPos,
        cy: yPos,

// second approach
        fill: data.lastEruption < 1000 ? "red" : data.lastEruption > 1000 && data.lastEruption < 1500? "blue": "orange",
        opacity: 0.5,
        stroke: "none",
        r: 5,
      }, 50);
    };
  }

// example out of my array 
var data = [
{
    "lastEruption": 1000,
  },
{ "lastEruption": 250,}
....]

数据库名称位于stats对象中。

答案 5 :(得分:0)

从MongoDB 4.4开始,这似乎已经改变。您可以通过databaseName属性访问当前数据库名称:

@override
  Widget build(BuildContext context) {
    final theme = Theme.of(context);
    if (!controller.value.isInitialized) {
      return Container(
          color: theme.colorScheme.onPrimary,
          child: Center(child: CircularProgressIndicator()));
    }

    return Scaffold(
      appBar: CupertinoNavigationBar(
        backgroundColor: theme.colorScheme.primary,
        border: Border.symmetric(
            vertical: BorderSide.none, horizontal: BorderSide.none),
        automaticallyImplyLeading: false,
        leading: IconButton(
          icon: Icon(
            Icons.chevron_left,
            size: 30,
            color: theme.colorScheme.onPrimary,
          ),
          onPressed: () => Navigator.pop(context),
        ),
        middle: Text("Memories",
            style: TextStyle(
                color: theme.colorScheme.onPrimary,
                fontSize: theme.textTheme.headline3.fontSize)),
      ),
      body: Container(
        child: Column(
          children: [
            Expanded(
              child: Camera(
                mode: CameraMode.normal,
                imageMask: lastPicture != null
                    ? new Positioned.fill(
                        child: new Opacity(
                          opacity: 0.3,
                          child: RotatedBox(
                            quarterTurns: 1,
                            child: new Image.file(
                              File(lastPicture),
                              fit: BoxFit.cover,
                            ),
                          ),
                        ),
                      )
                    : Container(),
                onFile: (File file) {
                  _workWithImage(file);
                },
              ),
            ),
          ],
        ),
      ),
    );
  }