gpt4 book ai didi

node.js - 有没有办法从 Node.js 驱动程序检查 Mongo 中是否存在数据库?

转载 作者:IT老高 更新时间:2023-10-28 13:21:16 24 4
gpt4 key购买 nike

我发现很难从 Node.js 驱动程序中找到一种方法来查看 MongoDB 中是否存在数据库。 似乎 Node.js 驱动程序中没有检查数据库是否存在的方法。

例如,以下不会引发错误:

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

mongo.connect({ 'mongodb://localhost:27017/databaseThatDoesntExists }, function (err, db) {
// There is no error
if (err) console.log(err);
// Let's get some stats on a database that doesn't exist
db.statsAsync(function (err, result) {
console.log(result);
});
});

结果将是这样的对象:

{ db: 'databaseThatDoesntExist',
collections: 0,
objects: 0,
avgObjSize: 0,
dataSize: 0,
storageSize: 0,
numExtents: 0,
indexes: 0,
indexSize: 0,
fileSize: 0,
ok: 1 }

您能否通过 Node.js 驱动程序检查 MongoDB 中是否存在数据库? MongoDB中是否存在数据库的概念?引用一个不存在的数据库只是引用一个没有集合的数据库吗?

Mongo,你为什么不能直接抛出一个错误!我喜欢我的代码 throws 错误!

最佳答案

检查 db.admin().listDatabases包含数据库名称。

如果服务器有 authorization启用,您将需要适当的 permissions .

关于node.js - 有没有办法从 Node.js 驱动程序检查 Mongo 中是否存在数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29997020/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com