簡體   English   中英

在 Mongo 指南針中查看 Mongo DB

[英]View Mongo DB in Mongo compass

我是MongoDBNode新手。 我已使用以下方法連接到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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM