我正在子文档中的Mongoose中尝试执行findOne查询,但运气不佳...

我的架构如下所示:

var Team = mongoose.Schema({
    teamName:       String,
    teamURL:        String,
    teamMembers:    [{username: String, password: String, email: String, dateCreated: Date}],
});

var Team = db.model('Team', Team);

我只需要从我使用此查询的文档中找到用户的电子邮件
Team.findOne({'teamMembers.username': 'Bioshox'}, {'teamMembers.$': 1}, function (err, team) {
    if (team) {
        console.log(team[1].email);
    }
});

任何帮助,将不胜感激!

最佳答案

您缺少对象的teamMembers级别,因此您的代码需要更改为以下内容:

Team.findOne({'teamMembers.username': 'Bioshox'}, {'teamMembers.$': 1},
    function (err, team) {
        if (team) {
            console.log(team.teamMembers[0].email);
        }
    }
);

关于node.js - Mongoose 中的findOne子文档,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13460765/

10-10 19:20