更新Meteor中的现有Mongo集合对象

时间:2015-01-15 18:51:23

标签: mongodb meteor

我看到了其他答案,例如this,但我认为我的答案更具体一些。

Meteor.user()作为Object {_id: "iymu2h9uysCiKFHvc", emails: Array[2], profile: Object, services: Object}

我正在运行一个函数来设置配置文件的名字和姓氏:

thisId = Meteor.userId();

Meteor.users.update({ _id: thisId }, { $set: {
  profile: {
    first_name: $('#firstName').val(),
    last_name: $('#lastName').val()
  }
}
});

但是,我也希望在不同的事件中向配置文件添加一个notifications对象。 我试过了:

 thisId = Meteor.userId();

  Meteor.users.update({ _id: thisId }, { $set: {
    profile: {
      notifications: {
        rcptDwnldFile: Session.get('recpt-dwnld-file'),
        rcptPaysInv: Session.get('recpt-pays-inv'),
        invSentDue: Session.get('inv-sent-due'),
        // the rest
      }
    }
  }
});

但是会覆盖我的first_namelast_name条目。我也试过了$setOnInstert,但我得到了update failed: Access denied. Operator $setOnInsert not allowed in a restricted collection.,但我认为默认情况下用户可以写profile

1 个答案:

答案 0 :(得分:0)

使用此代码(更多信息link - 请参阅在嵌入式文档中设置字段部分):

thisId = Meteor.userId();

Meteor.users.update({ _id: thisId }, { $set: {
    'profile.first_name': $('#firstName').val(),
    'profile.last_name': $('#lastName').val()  
}
});

thisId = Meteor.userId();

Meteor.users.update({ _id: thisId }, { $set: {
        'profile.notifications.rcptDwnldFile': Session.get('recpt-dwnld-file'),
        'profile.notifications.rcptPaysInv': Session.get('recpt-pays-inv'),
        'profile.notifications.invSentDue': Session.get('inv-sent-due'),
        // the rest          
  }
});
相关问题