I'm trying to figure out where in the project structure I can create a mongoDB document instance for which I defined a schema before.
I defined my connection in the main index.js file for mongoDB:
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var express = require('express');
mongoose.connect("mongodb://localhost/db");
and created the schema and document model of the schema inside the "/model" subfolder. I also created an instance of the document schema model and used the method .save() on it, however, nothing happened when I looked inside MongoDB Compass. No database or collection was created.
var mySchema = new mongoose.Schema({name: String, count: Number});
var Test = mongoose.model("Test", mySchema);
var test1 = new Test({name: "Clinton", count: 5});
test1.save(function (err) {
if (err) return handleError(err);
// saved!
});;
Can someone explain me what I have to do to see the created documents, or where I have to define my document instances inside the Node.js structure?
I use express 4.0 with mongoose and some predefined template structures, I am pretty new to the Node.js stack and so I am not sure if I get it right with the complex structure. I started the program in cmd with node index.js. It started and listened on port 3000, but not database or collections / documents were created...