2016-09-29 4 views
0

나는 모든 모임에 대해 전체 DB의 값을 업데이트하려고합니다. MongoCollection 업데이트 값이 무언가 같음

{ 
    "Referent" : null 
    "Participants" : [ 
     { 
      "Email" : "[email protected]", 
      "Present" : true 
     }, 
     { 
      "Email" : "[email protected]", 
      "Present" : false 
     }, 
     { 
      "Email" : "[email protected]", 
      "Present" : true 
     } 
    ] 
} 

내가 이런 일 할 : 현재는 다음과 같습니다

if(meeting.Referent == null) { 
    foreach(var participant in meeting.Partipants) { 
     if(participant.Present) { 
      meeting.Referent = participant.Email; 
     } 
    } 
} 

은 물론 위의 코드는 MongoCollection 작동하지 않습니다,하지만 난 그게 말이 바랍니다. 회의에 참석 한 무작위 (첫 번째 또는 마지막) 참가자에게 회의 지시 대상을 설정하려고합니다.

MongoCollection을 사용하면 어떻게 할 수 있습니까? 그래서 Mongo Shell에서 실행할 수 있습니까?

답변

0

오, 좋았어, 난 당신이 몽고 쉘에 자바 스크립트를 입력 할 수 있다는 것을 알지 못했다. 완벽하게

var meetings = db.meetings.find({ 
     Referent: null 
}).toArray(); 

meetings.forEach(function(meeting) { 
    if(meeting.Participants.length > 0) { 
     meeting.Participants.forEach(function(participant) { 
      if(participant.Present) { 
       meeting.Referent = participant.Email; 
      } 
     }); 
     if(meeting.Referent == null) { 
      meeting.Referent = meeting.Participants[0].Email; 
     } 
     db.meetings.updateOne({ 
      "_id": meeting._id 
     }, { 
      $set: {"Referent": meeting.Referent } 
     }); 
    } 
}); 

작품 :

: 이것은 내가 함께 결국 무엇인가