-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDay23.js
50 lines (37 loc) · 1.21 KB
/
Day23.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
const express = require('express');
const mongoose = require('mongoose')
const app = express();
mongoose.connect('mongodb://127.0.0.1:27017/day23DB')
.then(() => console.log("database connection successful"))
.catch((err) => console.error("connection error: ", err))
const productWithCategorySchema= new mongoose.Schema({
name: {type:String, required: true},
price: {type:Number, required:true},
category: {
type:String,
required: true,
enum: ['electronics', 'homeAppliances', 'clothes', 'books']
},
publishedDate: {type:Date, default: Date.now},
});
//models
const ProductWithCategory = mongoose.model("ProductWithCategory", productWithCategorySchema);
async function createProductWithCategory(){
const product = new ProductWithCategory({
name: 'novel',
price: 500,
category:'books',
});
try{
const result = await product.save()
console.log(result)
}catch(err){
console.error(err.message)
}
}
createProductWithCategory();
async function getProductWithCategory(){
const products = await ProductWithCategory.find({category: 'electronics'})
console.log(products)
}
getProductWithCategory();