const express = require('express') const mongoose = require("mongoose")
const app = express()
app.use(express.json())
app.use(express.urlencoded({ extended: false }))
mongoose.connect("mongodb://127.0.0.1:27017/lam_test")
const userType = { name: String, age: Number, }
const userModel = mongoose.model('user', new mongoose.Schema(userType))
app.post('/user/add', (req, res) => { console.log('新增的数据:', req.body);
userModel.insertMany({ name: req.body.name, age: req.body.age }).then(data => { console.log("成功插入数据:", data); res.send({ ok: 1 }) }) })
app.post('/user/update', (req, res) => { console.log('要修改的数据:', req.body);
userModel.updateMany({ name: req.body.firstName }, { $set: { name: req.body.name, age: req.body.age } }).then(data => { console.log("成功修改数据:", data); res.send({ ok: 2 }) }) })
app.post('/user/delete', (req, res) => { console.log('要删除的数据:', req.body);
userModel.deleteMany({ name: req.body.name, age: req.body.age }).then(data => { console.log("成功删除数据:", data); res.send({ ok: 3 }) }) })
app.post('/user/find', (req, res) => { console.log('要查找的数据:', req.body);
userModel.find({ name: req.body.name }, { age: 1 }, { skip: 0, limit: 1 }).then(data => { console.log("成功查找到数据:", data); res.send({ ok: 4 }) }) })
app.listen(3000, () => { console.log('服务器已启动,3000端口号正在监听...'); })
|