【问题标题】:View Mongo DB in Mongo compass在 Mongo 指南针中查看 Mongo DB
【发布时间】:2020-08-27 22:09:39
【问题描述】:

我是 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 })

【问题讨论】:

  • 如何准确地看到数据库?
  • 我想我的意思很清楚了..我想要我的数据的可视化表示。
  • 不是。您可以从数据库中检索文档,您的意思是查看我不知道的数据库。
  • 感谢您的意见。把它留给知道如何查看数据库及其记录的人
  • MongoDb 只有在至少有一个文档时才会创建数据库或集合。如果它为空,您将看不到集合或数据库。确保您已成功插入数据。

标签: javascript node.js mongodb express


【解决方案1】:

使用mongo shell 从数据库中插入和检索文档,验证它是否有效。

使用 compass 查看您通过 shell 插入的文档,验证此操作是否有效。

在您的应用程序中插入文档并通过 shell 检索它们,验证此操作是否有效。

此时,您的应用程序和指南针之间的任何问题都应该得到解决。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-30
    • 2020-10-03
    相关资源
    最近更新 更多