2015-05-04 3 views
0

Я строю систему управления списком друзей с метеор. Для этого я отправляю уведомление пользователю, который получает запрос. Затем, когда я его принимаю, я хочу изменить этот профиль пользователя, чтобы изменить статус запроса в профиле пользователя.Запись в коллекции в Метеор

Template.navigation.events({ 
    'click #ok-btn': function(e, tmpl){ 
     Meteor.users.update({_id: Meteor.userId()}, { 
     $set: { 
      'profile.friends': [...] 
     }}); 
     Meteor.call('removeNotif', Meteor.user()._id, this.id); 
    }); 

Знакомый объект массива профиля строится так:

[{id: foo, status: bar}, [...]] 

Как обновить статус здесь? Я не могу просто сделать

var friendList = Meteor.user().profile.friends, 
      i = 0; 
     for(i = 0; i < friendList.length; i++) 
     if(friendList.id == id) 
      break; 

     Meteor.users.update({_id: Meteor.userId()}, { 
     $set: { 
      'profile.friends'+[i]+'.satus': myNewStatus 
     }}); 

Как я должен продолжить?

ответ

1

Вы можете просто редактировать список друзей заранее и передать обновленные один к вашей методе обновления:

var friendList = Meteor.user().profile.friends, 
      i = 0; 
     for(i = 0; i < friendList.length; i++) { 
     if(friendList[i].id == id) { 
      friendList[i].status = myNewStatus; 
      break; 
     } 
     } 
     Meteor.users.update({_id: Meteor.userId()}, { 
     $set: { 
      'profile.friends': friendList 
     }}); 

Или, используя позиционный $ оператора:

Meteor.users.update({_id: Meteor.userId(), 'profile.friends.id': id}, { 
    $set: { 
    'profile.friends.$.status': myNewStatus 
    } 
}); 
1

Использования MongoDB positional operator $:

Meteor.users.update({_id: Meteor.userId()}, { 
    $set: { 
     'profile.friends.$.status': myNewStatus 
    } 
}); 
Смежные вопросы