[英]How can I format this mongoose updateone query
I am trying to update certain fields within a mongodb collection according the the values passed to the database function.我正在尝试根据传递给数据库 function 的值更新 mongodb 集合中的某些字段。
module.exports.update_sprint = async function update_sprint(teamname, update_data,
sprintnum, sprintStar) {
console.log(teamname)
console.log(update_data)
console.log(sprintnum)
console.log(sprintStar)
TeamM.updateOne({teamName: teamname, sprints: sprintnum}, {$set: {
'sprints.$.stars': update_data, 'sprints.$.sprintstar': sprintStar}},
function (error, success) {
if (error) {
console.log(error)
} else {
console.log(success)
}
})
Database format: enter image description here数据库格式:在此处输入图像描述
I am getting:我正进入(状态:
test123
[
{ name: 'test@lewisu.edu', stars: 1, points: '5' },
{ name: 'test2@lewisu.edu', stars: 0, points: 0 }
]
1
test@lewisu.edu
{
acknowledged: true,
modifiedCount: 0,
upsertedId: null,
upsertedCount: 0,
matchedCount: 0
}
as a result of the query but the values are not being updated in the database.作为查询的结果,但数据库中的值没有更新。
I cannot seem to find a way to structure this mongoose query correctly.我似乎找不到正确构建此 mongoose 查询的方法。
In your request u want to match sprints = sprintum, you just have to change it to this:在您的请求中,您想匹配 sprints = sprintum,您只需将其更改为:
Test.updateOne({ teamName: "test123", "sprints.sprintum": 1 }, { // Mistake was in this line
$set: {
'sprints.$.stars': update_data,
'sprints.$.sprintstar': sprintStar
}
})
Before:前:
{
"teamName": "test123",
"members": [
"test@lewisu.edu",
"test@lewisu.edu"
],
"scrumMaster": "test@lewisu.edu",
"totalMembers": 2,
"sprints": [
{
"sprintum": 1,
"stars": [
{
"name": "test@lewisu.edu",
"stars": 0,
"points": 0
},
{
"name": "test2@lewisu.edu",
"stars": 0,
"points": 0
}
],
"sprintstar": "None"
}
]
}
After:后:
As of your comment I added an example for multiple identifiers:根据您的评论,我为多个标识符添加了一个示例:
Test.updateOne({ teamName: "test123" }, {
$set: {
'sprints.$[i].stars.$[j].stars': 5,
'sprints.$[i].sprintstar': "test@lewisu.edu"
}
}, { arrayFilters: [{ "i.sprintum": 1 }, { "j.name": "test@lewisu.edu" }] })
This will update in the array with sprintum one the stars of the person in the stars array with the name "test@lewisu.edu"这将使用 sprintum 更新数组中的星星数组中名称为“test@lewisu.edu”的人的星星
With this you can basically do everything you want in all the arrays in your document有了这个,您基本上可以在文档中的所有 arrays 中做任何您想做的事情
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.