如何使用 node.js 中的 monk 更新 mongodb 中的嵌套字段
How to update a nested field in mongodb with monk in node.js
我在用新记录更新我的 mongodb 数据库时遇到了一些困难。数据库由作为数组的练习字段组成,当我执行 PUT header 时,我想用新记录更新特定练习,而不是像我想保留的那样覆盖特定练习的整个字段进步的历史。到目前为止,我只能获取特定的嵌套字段,如 router.get('/:nickname/:info') 中所示,我们可以在其中指定用户及其姓名、年龄或性别。我可以对练习字段做同样的事情,但我总是无法更新这些字段。这是数据库:
[
{
"_id": "59283956c52a5420e74966b9",
"info": {
"nickname": "annwumy",
"name": "Bert",
"gender": "male",
"age": 25
},
"exercises": [
{
"exercise": {
"name": "squats",
"records": []
}
},
{
"exercise": {
"name": "legpresses",
"records": []
}
},
{
"exercise": {
"name": "deadlifts",
"records": []
}
},
{
"exercise": {
"name": "benchpresses",
"records": []
}
},
{
"exercise": {
"name": "pullups",
"records": []
}
},
{
"exercise": {
"name": "shoulderpress",
"records": []
}
},
{
"exercise": {
"name": "curls",
"records": []
}
}
]
},
{
"_id": "5928397fc52a5420e74966ba",
"info": {
"nickname": "mmsmsy",
"name": "Mateusz",
"gender": "male",
"age": 26
},
"exercises": [
{
"exercise": {
"name": "squats",
"records": []
}
},
{
"exercise": {
"name": "legpresses",
"records": []
}
},
{
"exercise": {
"name": "deadlifts",
"records": []
}
},
{
"exercise": {
"name": "benchpresses",
"records": []
}
},
{
"exercise": {
"name": "pullups",
"records": []
}
},
{
"exercise": {
"name": "shoulderpress",
"records": []
}
},
{
"exercise": {
"name": "curls",
"records": []
}
}
]
}
]
这是带有请求的 back-end 代码:
const express = require('express');
const router = express.Router();
const app = express();
const cors = require('cors');
const bodyParser = require('body-parser');
const axios = require('axios');
const monk = require('monk');
const db = require('monk')('localhost/fitness-log');
const users = db.get('users');
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use('/api/v1', router);
router.get('/listusers', (req, res) => {
users.find()
.then(users => res.json(users))
});
router.get('/users/:id', (req, res) => {
users.find({'_id': req.params.id})
.then(user => res.json(user[0]))
});
router.get('/:nickname/:info', (req, res) => {
users.find({'nickname': req.params.nickname}, `info.${req.params.info}`)
.then(user => res.json(user[0]))
});
router.post('/adduser', (req, res) => {
users.insert(req.body, (err, result) => {
res.send(
(err === null) ? { msg: result } : { msg: err }
);
});
});
router.put('/updateinfo/:nickname/:info', (req, res) => {
const updateQuery = {$set: {}};
updateQuery.$set[`info.${req.params.info}`] = req.body.value;
users.update({'nickname': req.params.nickname}, updateQuery, (err, result) => {
res.send(
(err === null) ? { msg: result } : { msg: err }
);
});
});
router.put('/addrecord/:nickname/:exercise', (req, res) => {
const updateQuery = {$push: {}};
updateQuery.$push[`exercises.${req.params.exercise}`] = req.body;
users.update({'nickname': req.params.nickname}, updateQuery, (err, result) => {
res.send(
(err === null) ? { msg: result } : { msg: err }
);
});
});
router.delete('/deleteuser/:nickname', (req, res) => {
users.remove({ 'nickname': req.params.nickname }, err => {
res.send((err === null) ? { msg: '' } : { msg: 'error: ' + err});
});
});
router.delete('/deleteallusers', (req, res) => {
users.remove({}, err => {
res.send(err === null) ? {msg: '' } : { msg: 'error: ' + err};
});
});
app.listen(3001, () => console.log("Server running at 3001"));
错误在更新表达式中:
{$set: `info.${req.params.info}`}
必须是:
const updateQuery = {$set: {}};
updateQuery.$set[`info.${req.params.info}`] = req.params.infoValue;
users.update({'nickname': req.params.nickname}, updateQuery, (err, result) => {
我在用新记录更新我的 mongodb 数据库时遇到了一些困难。数据库由作为数组的练习字段组成,当我执行 PUT header 时,我想用新记录更新特定练习,而不是像我想保留的那样覆盖特定练习的整个字段进步的历史。到目前为止,我只能获取特定的嵌套字段,如 router.get('/:nickname/:info') 中所示,我们可以在其中指定用户及其姓名、年龄或性别。我可以对练习字段做同样的事情,但我总是无法更新这些字段。这是数据库:
[
{
"_id": "59283956c52a5420e74966b9",
"info": {
"nickname": "annwumy",
"name": "Bert",
"gender": "male",
"age": 25
},
"exercises": [
{
"exercise": {
"name": "squats",
"records": []
}
},
{
"exercise": {
"name": "legpresses",
"records": []
}
},
{
"exercise": {
"name": "deadlifts",
"records": []
}
},
{
"exercise": {
"name": "benchpresses",
"records": []
}
},
{
"exercise": {
"name": "pullups",
"records": []
}
},
{
"exercise": {
"name": "shoulderpress",
"records": []
}
},
{
"exercise": {
"name": "curls",
"records": []
}
}
]
},
{
"_id": "5928397fc52a5420e74966ba",
"info": {
"nickname": "mmsmsy",
"name": "Mateusz",
"gender": "male",
"age": 26
},
"exercises": [
{
"exercise": {
"name": "squats",
"records": []
}
},
{
"exercise": {
"name": "legpresses",
"records": []
}
},
{
"exercise": {
"name": "deadlifts",
"records": []
}
},
{
"exercise": {
"name": "benchpresses",
"records": []
}
},
{
"exercise": {
"name": "pullups",
"records": []
}
},
{
"exercise": {
"name": "shoulderpress",
"records": []
}
},
{
"exercise": {
"name": "curls",
"records": []
}
}
]
}
]
这是带有请求的 back-end 代码:
const express = require('express');
const router = express.Router();
const app = express();
const cors = require('cors');
const bodyParser = require('body-parser');
const axios = require('axios');
const monk = require('monk');
const db = require('monk')('localhost/fitness-log');
const users = db.get('users');
app.use(cors());
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use('/api/v1', router);
router.get('/listusers', (req, res) => {
users.find()
.then(users => res.json(users))
});
router.get('/users/:id', (req, res) => {
users.find({'_id': req.params.id})
.then(user => res.json(user[0]))
});
router.get('/:nickname/:info', (req, res) => {
users.find({'nickname': req.params.nickname}, `info.${req.params.info}`)
.then(user => res.json(user[0]))
});
router.post('/adduser', (req, res) => {
users.insert(req.body, (err, result) => {
res.send(
(err === null) ? { msg: result } : { msg: err }
);
});
});
router.put('/updateinfo/:nickname/:info', (req, res) => {
const updateQuery = {$set: {}};
updateQuery.$set[`info.${req.params.info}`] = req.body.value;
users.update({'nickname': req.params.nickname}, updateQuery, (err, result) => {
res.send(
(err === null) ? { msg: result } : { msg: err }
);
});
});
router.put('/addrecord/:nickname/:exercise', (req, res) => {
const updateQuery = {$push: {}};
updateQuery.$push[`exercises.${req.params.exercise}`] = req.body;
users.update({'nickname': req.params.nickname}, updateQuery, (err, result) => {
res.send(
(err === null) ? { msg: result } : { msg: err }
);
});
});
router.delete('/deleteuser/:nickname', (req, res) => {
users.remove({ 'nickname': req.params.nickname }, err => {
res.send((err === null) ? { msg: '' } : { msg: 'error: ' + err});
});
});
router.delete('/deleteallusers', (req, res) => {
users.remove({}, err => {
res.send(err === null) ? {msg: '' } : { msg: 'error: ' + err};
});
});
app.listen(3001, () => console.log("Server running at 3001"));
错误在更新表达式中:
{$set: `info.${req.params.info}`}
必须是:
const updateQuery = {$set: {}};
updateQuery.$set[`info.${req.params.info}`] = req.params.infoValue;
users.update({'nickname': req.params.nickname}, updateQuery, (err, result) => {