// Connecting to MongoDB
const connectionString = 'mongodb://127.0.0.1:27017/mensWear20DB';
mongoose.connect(connectionString, {
useNewUrlParser: true,
useUnifiedTopology: true
})
.then(() => console.log('Connected to MongoDB'))
.catch(err => console.error('Connection error', err));
// Product Schema
const productSchema = new mongoose.Schema({
name: { type: String, required: true },
price: { type: Number, required: true, min: 0 },
category: { type: String, required: true },
inStock: { type: Boolean, default: true }
});
// Creating the MODEL
const Product = mongoose.model('Product', productSchema);
// Use the Product model here
const product = new Product({
name: 'Shirt',
price: 29.99,
category: 'Tops',
inStock: true
});
product.save().then(() => {
console.log('Product saved');
mongoose.connection.close();
});
// Exporting the Model
module.exports = Product;