2017-04-17 5 views
0

Hallo habe ich die folgende Sammlung Struktur:Fetch ein Feld von Array MongoDB Meteor

{ 
 
    "_id" : "HZw2ktDPm6EWnGaFt", 
 
    "createdAt" : ISODate("2017-04-16T17:40:59.055Z"), 
 
    "pollName" : "", 
 
    "entryOwner" : "eHPeQPMd94MQFNXmg", 
 
    "question" : [ 
 
     { 
 
      "name" : "Question 1", 
 
      "questionId" : "sdPzbn9SWjE46HtM2" 
 
     }, 
 
     { 
 
      "name" : "Question 2", 
 
      "questionId" : "vpMrpbJ2LZKMLEYKe" 
 
     } 
 
    ], 
 
    "sharedWith" : [ 
 
     { 
 
      "id" : "jjX5EDdqMtcyQwd6h", 
 
      "name" : "person 1", 
 
      "votes" : 0 
 
     }, 
 
     { 
 
      "id" : "b3Ctr6LFZMd9smd4B", 
 
      "name" : "person 2", 
 
      "votes" : 0 
 
     } 
 
    ], 
 
    "voters" : [ 
 
     { 
 
      "voterId" : "eHPeQPMd94MQFNXmg", 
 
      "questionId" : "vpMrpbJ2LZKMLEYKe", 
 
      "optionId" : "EKnYKXEFBWnr4hnCP", 
 
      "peopleId" : "b3Ctr6LFZMd9smd4B" 
 
     }, 
 
     { 
 
      "voterId" : "eHPeQPMd94MQFNXmg", 
 
      "questionId" : "vpMrpbJ2LZKMLEYKe", 
 
      "optionId" : "EKnYKXEFBWnr4hnCP", 
 
      "peopleId" : "jjX5EDdqMtcyQwd6h" 
 
     }, 
 
     { 
 
      "voterId" : "eHPeQPMd94MQFNXmg", 
 
      "questionId" : "sdPzbn9SWjE46HtM2", 
 
      "optionId" : "rjYLitibXDJjGYKM7", 
 
      "peopleId" : "b3Ctr6LFZMd9smd4B" 
 
     }, 
 
     { 
 
      "voterId" : "eHPeQPMd94MQFNXmg", 
 
      "questionId" : "Q6JiaGFAi2LRHS7GQ", 
 
      "optionId" : "wFoduKp23cSYJJG9i", 
 
      "peopleId" : "b3Ctr6LFZMd9smd4B" 
 
     } 
 
    ] 
 
}

Ich mag würde den Wert von Voters.peopleId erhalten, indem diese Werte verwenden.

 "voterId" : "eHPeQPMd94MQFNXmg", 
     "questionId" : "vpMrpbJ2LZKMLEYKe", 
     "optionId" : "EKnYKXEFBWnr4hnCP", 

habe ich versucht, dieses und es hat nicht funktioniert es das ganze Dokument zurückgibt, aber was ich will als eine Rückkehr ist nur ein Feld:

var getPeopleId = Polls.findOne({ 
 
     _id:this.props.poll._id}, {"voters": { 
 
      $elemMatch :{voterId:Meteor.userId(),questionId:selectedQuestionId,optionId:selectedOptionId}}})

Vielen Dank

Antwort

0

Sie haben in der Projektion (2. Param) anstelle der Abfrage (1. Param). Sie müssen das Ergebnis auch so projizieren, dass nur die erste Übereinstimmung berücksichtigt wird. Versuchen Sie:

const poll = Polls.findOne(
    { 
    _id:this.props.poll._id, 
    voters: { 
     $elemMatch: { 
     voterId: Meteor.userId(), 
     questionId: selectedQuestionId, 
     optionId: selectedOptionId 
    } 
    },{ 
    'voters.$': 1 
    }); 
// guard against missing keys or no results 
const peopleId = poll && poll.voters && poll.voters.peopleId; 
+0

danke für die Antwort, aber es gibt immer noch alle Felder zurück. – picacode