I'm new to MongoDB
and Node
. I have connected to Mongo
using below
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)
});
And I created the below model
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;
EDIT
Inserting Data
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();
}
)
})
})
And I tried to save a new record to this model and I tested it using mocha
and it saved successfully. Now I want to view my database and my records using Mongo compass
.
I tried to connect to mongodb://localhost/testaroo
on port 27017
but I couldn't see my database. So what I'm missing here?
FIXED
For future viewers I fixed it by passing this option useUnifiedTopology
: true to the to the MongoClient constructor
mongoose.connect('mongodb://localhost/Mydb', { useNewUrlParser: true, useUnifiedTopology: true })
Use mongo
shell to insert and retrieve documents from the database, verify this works.
Use compass to view documents you inserted via the shell, verify this works.
Insert documents in your application and retrieve them via the shell, verify this works.
At this point whatever issue you have between your application and compass should be resolved.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.