initial
This commit is contained in:
commit
74ae93a96e
1 changed files with 94691 additions and 0 deletions
|
|
@ -0,0 +1,57 @@
|
|||
mongoose
|
||||
https://mongoosejs.com/docs/api/model.html
|
||||
|
||||
|
||||
- findall
|
||||
`const tasks = await Task.find()`
|
||||
|
||||
- sorting
|
||||
`const tasks = await Task.find().sort({complexity: 1})`
|
||||
- patching
|
||||
`const updatedTask = await Task.findByIdAndUpdate(taskID,{complexity},{new: true})`
|
||||
- inserting using save method
|
||||
```js
|
||||
const newTask = new Task({title,complexity})
|
||||
const savedTask = await newTask.save()
|
||||
```
|
||||
- deleting
|
||||
`const deletedTask = await Task.findByIdAndDelete(taskID)`
|
||||
|
||||
|
||||
basic program
|
||||
|
||||
```js
|
||||
const express = require('express')
|
||||
|
||||
const mongoose = require('mongoose')
|
||||
const cors = require('cors')
|
||||
require('dotenv').config()
|
||||
|
||||
const app = express()
|
||||
app.use(cors())
|
||||
app.use(express.json())
|
||||
|
||||
const PORT = process.env.PORT || 5000
|
||||
const MONGO_URI= process.env.MONGO_URI || 'mongodb://127.0.0.1:27017/merndb'
|
||||
|
||||
mongoose.connect(MONGO_URI).then(()=> console.log('mongodb connnected')).catch(err=>console.log('mongodb connection error'))
|
||||
|
||||
const TaskSchema = new mongoose.Schema({title: {type: String, require: true}})
|
||||
const Task = mongoose.model('Task',TaskSchema)
|
||||
|
||||
app.get('/api/tasks', async (req,res)=>{
|
||||
const tasks = await Task.find();
|
||||
res.json(tasks);
|
||||
|
||||
|
||||
})
|
||||
|
||||
|
||||
app.post('/api/tasks', async (req,res)=>{
|
||||
const newTask = new Task({title : req.body.title})
|
||||
const savedTask = await newTask.save()
|
||||
res.status(201).json(savedTask)
|
||||
} )
|
||||
|
||||
app.listen(PORT,()=> console.log(`Server running on port ${PORT}`) )
|
||||
```
|
||||
Loading…
Reference in a new issue