[英]View Mongo DB in Mongo compass
我是MongoDB
和Node
新手。 我已使用以下方法连接到Mongo
const mongoose = require("mongoose")
mongoose.connect('mongodb://localhost/testaroo', {
useNewUrlParser: true
})
mongoose.connection.once('open', function() {
console.log("Connection has been made ..")
}).on('error', function(error) {
console.log("Connection error", error)
});
我创建了以下模型
const mongoose = require('mongoose')
const schema = mongoose.Schema;
const marioCharSchema = new schema({
name: String, // Optional
weight: Number // Optional
})
const marioCharModel = mongoose.model('mariochar', marioCharSchema);
module.exports = marioCharModel;
编辑
插入数据
const assert = require('assert');
const mariochar = require('../test/models/mariochar')
// Describe tests
describe('Saving records', function () {
//Create tests
it("Saving records to database", function (done) {
const char = new mariochar({
name = "Test",
weight: 45
});
char.save().then(
function () {
assert(char.isNew === false)
done();
}
)
})
})
我尝试将新记录保存到此模型中,并使用mocha
对其进行了测试并成功保存。 现在我想使用Mongo compass
查看我的数据库和我的记录。
我试图在端口27017
上连接到mongodb://localhost/testaroo
,但我看不到我的数据库。 那么我在这里缺少什么?
固定的
对于未来的观众,我通过将这个选项useUnifiedTopology
: true 传递给 MongoClient 构造函数来修复它
mongoose.connect('mongodb://localhost/Mydb', { useNewUrlParser: true, useUnifiedTopology: true })
使用mongo
shell 从数据库中插入和检索文档,验证这是否有效。
使用指南针查看您通过 shell 插入的文档,验证它是否有效。
在您的应用程序中插入文档并通过 shell 检索它们,验证这是否有效。
此时,您的应用程序和指南针之间的任何问题都应该得到解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.