[英]Dynamically update key in array of objects + mongoose
I wouldnt be surprised if this has been asked before but I have not found a working example.如果之前有人问过这个问题,我不会感到惊讶,但我还没有找到一个有效的例子。
Basically I have a set of Boolean data inside of an array of objects and I would like to reuse my API routes/logic to update array of objects dynamically基本上我在对象数组中有一组布尔数据,我想重用我的 API 路由/逻辑来动态更新对象数组
Data Example:数据示例:
{
"_id": 1,
"posts": [
{ "_id": d323d32, "published": true, "homepage": false, (...moreBooleanData) },
{ "_id": ffwfwfwc, "published": true, "homepage": false, (...moreBooleanData) },
{ "_id": fdscsdad, "published": true, "homepage": false, (...moreBooleanData) }
]
}
Mongoose Query猫鼬查询
await Project.findOneAndUpdate(
{ _id: 1 },
{ $set: { "posts.$[el].published": isChecked } },
{
arrayFilters: [{ "el._id": postid }],
new: true
}
)
The problem is in this line "posts.$[el].published": isChecked
.问题出在这一行
"posts.$[el].published": isChecked
。 Here I have the key published
hard coded but I would like to have this key dynamic so I can grab it from the body of my post request在这里,我
published
硬编码的密钥,但我想拥有此密钥动态,以便我可以从我的帖子请求正文中获取它
const { DYNAMIC_KEY , isChecked } = req.body
"posts.$[el].$[DYNAMIC_KEY]": isChecked`
I have tried formatting the $set
string with backticks, building it outside the query and passing it as 1 variable all without success.我尝试用反引号格式化
$set
字符串,在查询之外构建它并将其作为 1 变量传递,但都没有成功。 Any ideas?有任何想法吗?
You can achieve this using bracket notation:您可以使用括号表示法实现此目的:
router.post("/project/:id/:postid", async (req, res) => {
const { isChecked, dynamicKey } = req.body;
let set = `posts.$[el].${dynamicKey}`;
console.log(set);
const result = await Project.findOneAndUpdate(
{ _id: req.params.id },
{ $set: { [set]: isChecked } },
{
arrayFilters: [{ "el._id": req.params.postid }],
new: true
}
);
res.send(result);
});
I have a project document with these 3 posts:我有一个包含这 3 个帖子的项目文档:
{
"_id" : ObjectId("5def81070066dc23e05b816e"),
"posts" : [
{
"_id" : ObjectId("5def81070066dc23e05b8171"),
"published" : true,
"homepage" : false
},
{
"_id" : ObjectId("5def81070066dc23e05b8170"),
"published" : true,
"homepage" : false
},
{
"_id" : ObjectId("5def81070066dc23e05b816f"),
"published" : true,
"homepage" : false
}
],
"__v" : 0
}
I send a post request to my router ../project/5def81070066dc23e05b816e/5def81070066dc23e05b8170
with this body:我向我的路由器
../project/5def81070066dc23e05b816e/5def81070066dc23e05b8170
发送了一个post请求:
{
"isChecked": false,
"dynamicKey": "published"
}
The result is like this: ( post's (with id 5def81070066dc23e05b8170) published value is updated to false)结果是这样的:(帖子的(id 5def81070066dc23e05b8170)发布值更新为false)
{
"_id": "5def81070066dc23e05b816e",
"posts": [
{
"_id": "5def81070066dc23e05b8171",
"published": true,
"homepage": false
},
{
"_id": "5def81070066dc23e05b8170",
"published": false,
"homepage": false
},
{
"_id": "5def81070066dc23e05b816f",
"published": true,
"homepage": false
}
],
"__v": 0
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.