CRUD with Mongoose
index.js -
const mongoose = require('mongoose');
const saveInDB = async () => {
await mongoose.connect('mongodb://localhost:27017/e-Comm');
const ProductSchema = new mongoose.Schema({
name: String,
brand: String,
price: Number,
category: String
});
const ProductsModel = mongoose.model('products', ProductSchema);
let data = new ProductsModel({ name: 'm9', brand: 'LG', price: 150, category: 'mobile' });
let result = await data.save();
console.log(result);
}
const updateInDB = async () => {
await mongoose.connect('mongodb://localhost:27017/e-Comm');
const ProductSchema = new mongoose.Schema({
name: String,
brand: String,
price: Number,
category: String
});
const Product = mongoose.model('products', ProductSchema);
let data = await Product.updateOne(
{ name: "m9" },
{ $set: { price: 5287, name:"M54" } }
)
console.log(data);
}
const findInDB = async () => {
await mongoose.connect('mongodb://localhost:27017/e-Comm');
const ProductSchema = new mongoose.Schema({
name: String,
brand: String,
price: Number,
category: String
});
const Product = mongoose.model('products', ProductSchema);
let data = await Product.find();
console.log(data);
}
findInDB();
// updateInDB()
//saveInDB()
Comments
Post a Comment