p2
const mongoose = require("mongoose");
mongoose.connect("mongodb://127.0.0.1:27017", {
dbName: "test",
useUnifiedTopology: true,
useNewUrlParser: true
}).then(() => {
console.log("Connected to database");
}).catch((err) => {
console.log(err);
});
const studentSchema = new mongoose.Schema({
name: String,
rollNo: Number,
class: String,
age: Number,
email: String
});
// Defining Student model
const Student = mongoose.model('Student', studentSchema);
// This step is optional. If the collection doesn't exist, Mongoose will create it automatically when you start saving documents.
// Commenting it out won't affect the functionality.
/*
Student.createCollection().then(function () {
console.log('Collection is created!');
});
*/
// Export the model
module.exports = Student;
-----------------------------------------------------------------
const mongoose = require("mongoose");
// Schema for collection
const studentSchema = new mongoose.Schema({
name: String,
rollNo: String, // Changed type to String
class: String,
contactNo: String, // Changed from age to contactNo
email: String
}, { collection: "students" });
// Exporting model
module.exports = mongoose.model("Student", studentSchema); // Capitalized "Student" to match with the model name
// Creating Schema
Comments
Post a Comment