使用嵌套数组时更新Meteor配置文件

时间:2017-10-16 18:56:04

标签: mongodb meteor

我在Meteor用户个人资料中有以下内容:

{
  "_id": "dwdtWPFjugF3sTLmE",
  "emails": [
    {
      "address": "armin@four1five.com",
      "verified": false
    }
  ],
  "profile": {
    "name": "Armin",
    "tasks": [
      {
        "task": "Task one",
        "completed": false,
        "created": "2017-10-16T18:48:21.331Z"
      },
      {
        "task": "Task Two",
        "completed": false,
        "created": "2017-10-16T18:48:25.898Z"
      }
    ]
  },
  "username": "armin"
}

如何通过“创建”查询,并更新“已完成”字段?或者我的做法有什么问题:

Meteor.users.update(
      { _id: Meteor.userId(), "profile.tasks.created": created },
      {
          $set: {
              'profile.tasks.completed': completed
          }
      }
    );

1 个答案:

答案 0 :(得分:2)

$set: {
    'profile.tasks.completed': completed
}

应该像positional operator '$'那样使用:

$set: {
    'profile.tasks.$.completed': completed
}