Node.js 从使用节点应用程序创建的shell访问mongo db
嗨,我是node和mongo DB开发的新手。我创建了一个简单的节点应用程序。我正在使用mongoDB。我已初始化mondo db并能够插入一些数据。在我的app.js中,我添加了DB连接,如下所示:Node.js 从使用节点应用程序创建的shell访问mongo db,node.js,database,mongodb,mongo-shell,Node.js,Database,Mongodb,Mongo Shell,嗨,我是node和mongo DB开发的新手。我创建了一个简单的节点应用程序。我正在使用mongoDB。我已初始化mondo db并能够插入一些数据。在我的app.js中,我添加了DB连接,如下所示: const mongoose = require('mongoose'); const mongoDB = 'mongodb://localhost:27017'; mongoose.connect(mongoDB); mongoose.Promise = global.Promise; cons
const mongoose = require('mongoose');
const mongoDB = 'mongodb://localhost:27017';
mongoose.connect(mongoDB);
mongoose.Promise = global.Promise;
const db = mongoose.connection;
我的模式类看起来像
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
let UserSchema = new Schema({
// publisherId: {type: String, required: true},
userName: {type: String, required: true},
age: {type: Number, required: true}
},{timestamps: true });
// Export the model
module.exports = mongoose.model('User', UserSchema);
现在我想从命令行访问相同的数据,所以我尝试连接我的数据库
mongo mongodb://localhost:27017
它与这个数据库连接时没有任何错误。当我尝试访问数据时,它不显示任何数据。我检查哪些数据库可用。它显示默认测试数据库。连接字符串中缺少数据库
mongo mongodb://localhost:27017
尝试以下方法:
const connect = async () => {
await mongoose.connect('mongodb://localhost/my-database')
}
我猜当连接字符串没有指定要使用的任何数据库时,mongoose正在使用default
作为默认值