【发布时间】:2018-08-09 03:39:53
【问题描述】:
我在本地主机上安装了 MongoDB 4.0。有以下 3 个默认数据库
> show dbs
admin 0.078GB
config 0.078GB
local 0.078GB
然后我尝试使用 nodejs 创建一个新数据库
var MongoClient = require('mongodb').MongoClient;
var url = "mongodb://local:27017/mydatabase"; // mydatabase is the name of db
MongoClient.connect(url, function(err, db) {
if (err) throw err;
console.log("Database created!");
db.close();
});
上面的代码工作正常。我可以看到消息“数据库已创建!”。
但是,当我再次显示 dbs 时,新创建的 mydatabase 不会出现。还有3个数据库。
> show dbs
admin 0.078GB
config 0.078GB
local 0.078GB
我可以知道我做错了什么吗?
谢谢 亚历克斯
【问题讨论】:
-
MongoDB waits until you have created a collection (table), with at least one document (record) before it actually creates the database (and collection).