从数组MongoDB Meteor中获取一个字段

时间:2017-04-17 17:14:13

标签: arrays mongodb meteor find

您好我有以下收集结构:

{
    "_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"
        }
    ]
}

我想通过使用这些值获得Voters.peopleId的值。

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

我尝试了这个并且它没有工作它返回整个文档但我想要的回报只是一个字段:

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

非常感谢

1 个答案:

答案 0 :(得分:0)

您在投影(第二个参数)中有specialButton而不是查询(第一个参数)。您还需要将结果投影到仅包含第一个匹配项。尝试:

$elemMatch