【发布时间】:2020-09-11 06:32:42
【问题描述】:
我正在尝试以类似的方式增加两个不同的数组。在不增加 elementC 的情况下,一切正常。如果我添加 elementC 并在其上使用 arrayFilter,则会收到错误消息:更新在 tasks.priority 处创建了冲突。我认为这与迭代同一个对象有关,但为什么以及如何解决这个问题?我的代码:
const project = await Project.updateOne(
{_id: req.params.project_id},
{
$set: {'tasks.$[elementA].priority': req.body.new_state.priority, 'tasks.$[elementA].state': req.body.new_state.state},
$inc: {'tasks.$[elementB].priority': 1, 'tasks.$[elementC].priority': 2},
},
{
arrayFilters: [
{'elementA._id': req.params.task_id},
{'elementB.state': req.body.old_state.state, 'elementB.priority': {$gt: req.body.old_state.priority}},
{'elementC.state': req.body.new_state.state, 'elementC.priority': {$gt: req.body.new_state.priority}}
]
}
);
res.json(project);
架构:
const task_schema = new mongoose.Schema({
description: {
type: String
},
state: {
type: String
},
priority: {
type: Number
}
const project_schema = new mongoose.Schema({
name: {
type: String
},
description: {
type: String
},
non_public: {
type: Boolean
},
tasks: [task_schema]
例子:
{ "_id" : ObjectId("5ec274b50877c671ccf9b6c7"),
"name" : "first project",
"description" : "lorem ipsum",
"non_public" : true,
"tasks" : [ { "_id" : ObjectId("5ec27674e0db347307902ba9"),
"description" : "Lorem",
"state" : "TODO",
"priority" : 0 },
{ "_id" : ObjectId("5ec27686e0db347307902baa"),
"description" : "Ipsum",
"state" : "TODO",
"priority" : 1 },
{ "_id" : ObjectId("5ec27686e0db347307902baa"),
"description" : "Dolor",
"state" : "TODO",
"priority" : 2 },
{ "_id" : ObjectId("5ec293db751ae07a8520a703"),
"description" : "Sit",
"state" : "DOING",
"priority" : 0 },
{ "_id" : ObjectId("5ec29591ad84927c7788116d"),
"description" : "Amet",
"state" : "DONE",
"priority" : 0 },
]}
【问题讨论】:
-
您能提供一些示例文件吗?您的预期输出是什么?
-
我有一个任务数组,每个任务都有一个优先级和一个状态。当我将任务从一个状态移动到另一个状态时。在它被删除的列表中,我需要通过减少它之后的所有其他任务来更新列表。在它被添加到的列表中,我需要增加它之后的所有任务。因此,我需要根据它们所处的状态对它们进行两次过滤,从而遍历同一个数组。
-
提供
req.params.task_id、req.body.old_state.state、req.body.old_state.priority、req.body.new_state.state、req.body.new_state.priority各自的值。 -
old_state.state 和 new_state.state 是“TODO”、“DOING”或“DONE”。 old_state.priority 和 new_state.priority 是一个整数。 old_state.priority 是它曾经在列表中的位置,new_state.priority 是它所在的新位置
标签: arrays node.js mongodb mongoose mongodb-query