nodejs+express+mongoose操作mongodb增删改查
时间: 2023-09-07 22:11:01 浏览: 211
nodejs操作mongodb实现增删改查
4星 · 用户满意度95%
1. 安装依赖
安装`express`、`mongoose`、`body-parser`和`cors`:
```
npm install express mongoose body-parser cors --save
```
2. 连接数据库
在`app.js`中连接数据库:
```javascript
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const cors = require('cors');
const app = express();
// 连接数据库
mongoose.connect('mongodb://localhost/test', { useNewUrlParser: true });
const db = mongoose.connection;
db.on('error', console.error.bind(console, '连接错误:'));
db.once('open', function() {
console.log('连接成功');
});
app.use(bodyParser.json());
app.use(cors());
app.listen(3000, () => console.log('服务器已启动'));
```
3. 定义模型
在`models`文件夹下创建`user.js`文件,定义用户模型:
```javascript
const mongoose = require('mongoose');
const userSchema = new mongoose.Schema({
name: String,
age: Number,
gender: String,
});
const User = mongoose.model('User', userSchema);
module.exports = User;
```
4. 增加数据
在`app.js`中增加路由,实现增加用户功能:
```javascript
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const cors = require('cors');
const User = require('./models/user');
const app = express();
// 连接数据库
mongoose.connect('mongodb://localhost/test', { useNewUrlParser: true });
const db = mongoose.connection;
db.on('error', console.error.bind(console, '连接错误:'));
db.once('open', function() {
console.log('连接成功');
});
app.use(bodyParser.json());
app.use(cors());
// 增加用户
app.post('/users', (req, res) => {
const user = new User({
name: req.body.name,
age: req.body.age,
gender: req.body.gender,
});
user.save((err, user) => {
if (err) {
console.error(err);
res.sendStatus(500);
} else {
res.json(user);
}
});
});
app.listen(3000, () => console.log('服务器已启动'));
```
5. 查询数据
在`app.js`中增加路由,实现查询用户功能:
```javascript
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const cors = require('cors');
const User = require('./models/user');
const app = express();
// 连接数据库
mongoose.connect('mongodb://localhost/test', { useNewUrlParser: true });
const db = mongoose.connection;
db.on('error', console.error.bind(console, '连接错误:'));
db.once('open', function() {
console.log('连接成功');
});
app.use(bodyParser.json());
app.use(cors());
// 增加用户
app.post('/users', (req, res) => {
const user = new User({
name: req.body.name,
age: req.body.age,
gender: req.body.gender,
});
user.save((err, user) => {
if (err) {
console.error(err);
res.sendStatus(500);
} else {
res.json(user);
}
});
});
// 查询用户
app.get('/users', (req, res) => {
User.find((err, users) => {
if (err) {
console.error(err);
res.sendStatus(500);
} else {
res.json(users);
}
});
});
app.listen(3000, () => console.log('服务器已启动'));
```
6. 更新数据
在`app.js`中增加路由,实现更新用户功能:
```javascript
const express = require('express');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const cors = require('cors');
const User = require('./models/user');
const app = express();
// 连接数据库
mongoose.connect('mongodb://localhost/test', { useNewUrlParser: true });
const db = mongoose.connection;
db.on('error', console.error.bind(console, '连接错误:'));
db.once('open', function() {
console.log('连接成功');
});
app.use(bodyParser.json());
app.use(cors());
// 增加用户
app.post('/users', (req, res) => {
const user = new User({
name: req.body.name,
age: req.body.age,
gender: req.body.gender,
});
user.save((err, user) => {
if (err) {
console.error(err);
res.sendStatus(500);
} else {
res.json(user);
}
});
});
// 查询用户
app.get('/users', (req, res) => {
User.find((err, users) => {
if (err) {
console.error(err);
res.sendStatus(500);
} else {
res.json(users);
}
});
});
// 更新用户
app.put('/users/:id', (req, res) => {
User.findByIdAndUpdate(req.params.id, {
$set: {
name: req.body.name,
age: req.body.age,
gender: req.body.gender,
},
}, { new: true }, (err, user) => {
if (err) {
console.error(err);
res.sendStatus(500);
} else {
res.json(user);
阅读全文