Can an mongodb update and $set look like this?


#1
Friends.update({
            "_id": Meteor.userId(),
             "friends.id1": personId1, 
             "friends.id2": Meteor.userId() }, 
            
            { $set: { "friends.$.status":'2' }  } 
           );

Friends.update({
            "_id": Meteor.userId(),
             "friends.id1": Meteor.userId(), 
             "friends.id2":  personId1}, 
            
            { $set: { "friends.$.status":'2' }  } 
           )

and two off them


#2

I’m not sure what your business logic is, but if you need to update the status on the same array element that has id1 and id2 satisfying your condition, then you should look at $elemMatch for your query. And unless you can have multiple array elements in the same Friends doc satisfying both conditions (in your two update statements), you should be able to $or both $elemMatch in the same query and use only one update statement.


Friends.update: updates the wrong one (the same everytime)
#3

The final code that work:

approveFriends: function(personId1) {
let meteorUser = Meteor.userId();

    Friends.update(
      {friends: {$elemMatch: {id1: personId1, id2: meteorUser}}},
     { $set: {"friends.$.status": 2} } 

    );

    Friends.update(
      {friends: {$elemMatch: {id1: meteorUser, id2: personId1}}},
      { $set: {"friends.$.status" : 2} }

    );
  }