[英]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.