[英]Is there a way to check if a database exists in Mongo from the Node.js driver?
我发现很难从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
包含数据库名称。
判断数据库是否存在的最简单方法是来自mongo。 要列出所有可用的数据库,请在Mongo shell中输入以下内容。
show dbs
要列出您当前使用的数据库,请输入:
db
要显式选择数据库,请输入:
use <database>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.