在子阵列中更新第三层文档

时间:2015-03-20 14:33:27

标签: mongodb meteor insert-update arrays subdocument

我有一个如下文档(在Projects集合中):

      title: "title"
      company: "company"
      companyID: "companyID"
      category: "category"
      starred: false
      createdAt: new Date
      tasks: [
        {
          task:'something'
          completed: false
          taskID: Random.id()
        }
        {
          task:'something'
          completed: false
          taskID: Random.id()
        }
        {
          task:'something'
          completed: false
          taskID: Random.id()
        }
      ]

我可以使用此方法在任务上设置completed: true

  completeTask: (projectID, taskID, ifCompleted) ->
    Projects.update {
      _id: projectID
      'tasks.taskID': taskID
    }, $set: 'tasks.$.completed': ifCompleted
    return

如果我将文档更改为:

{
  title: 'title.com'
  company: 'company'
  companyID: Random.id()
  category: 'category'
  starred: false
  createdAt: new Date
  categories: [
      {
        completed: true
        name: 'ideas'
        _id: Random.
        tasks: [
          {task: 'something', completed: false, taskID: Random.id()}
          {task: 'something', completed: false, taskID: Random.id()}
          {task: 'something', completed: false, taskID: Random.id()}
        ]
      }
      {
        completed: false
        name: 'development'
        _id: Random.id()
        tasks: [
          {task: 'something', completed: false, taskID: Random.id()}
          {task: 'something', completed: false, taskID: Random.id()}
          {task: 'something', completed: false, taskID: Random.id()}
        ]
      }
    ]
}

如何在任务上设置completed: true

到目前为止我已经

completeTask: (projectID, categoryID, taskID, ifCompleted) ->
    Projects.update {

0 个答案:

没有答案