gpt4 book ai didi

javascript - 如何使用node.js中的monk更新mongodb中的嵌套字段

转载 作者:太空宇宙 更新时间:2023-11-04 00:22:08 25 4
gpt4 key购买 nike

我在使用新记录更新 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": []
}
}
]
}
]

这是带有请求的后端代码:

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) => {

关于javascript - 如何使用node.js中的monk更新mongodb中的嵌套字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44189190/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com